servlet没法获得数据
regist.jsp之前写的是post然后,在servlet这里获得不到信息,打印用户列表都是Null,然后我吧post改成get就可以获得数据了,但是老师怎么就可以直接得到前台穿过来的数据呢?第一个有用户数据的是之前测试get的时候得到的,后面null的是post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.imooc.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.domain.User; import com.imooc.service.UserService; import com.imooc.service.impl.UserServiceImpl; /** * Servlet implementation class RegistServlet */ @WebServlet ( "/RegistServlet" ) public class RegistServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接受数据 String username = request.getParameter( "username" ); String password = request.getParameter( "password" ); //封装数据 User user= new User(); user.setUsername(username); user.setPassword(password); //处理数据 UserService userService = new UserServiceImpl(); List<User> userList=(List<User>)getServletContext().getAttribute( "userList" ); userService.regist(userList, user); System.out.println(userList); //显示处理结果 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title>用户注册</title> <link href= "style/common.css" type= "text/css" rel= "stylesheet" > <link href= "style/add.css" type= "text/css" rel= "stylesheet" > <link rel= "stylesheet" href= "style/login.css" > <script type= "text/javascript" > function validate_form(){ // 获得用户名的值 var username = document.getElementById( "username" ).value; if (username == null || username== "" ){ alert( "用户名不能为空" ); return false ; } // 获得密码的值 var password = document.getElementById( "password" ).value; if (password == null || password== "" ){ alert( "密码不能为空" ); return false ; } // 获得确认密码的值 var repassword = document.getElementById( "repassword" ).value; if (repassword != password){ alert( "两次密码输入不一致" ); return false ; } return true ; } </script> </head> <body> <%@ include file= "header.jsp" %> <div class = "content" > <div class = "center" > <div class = "center-login" > <div class = "login-banner" > <a href= "#" ><img src= "image/login_banner.png" alt= "" ></a> </div> <div class = "user-login" > <div class = "user-box" > <div class = "user-title" > <p>用户注册</p> </div> <form id= "regForm" enctype= "multipart/form-data" onsubmit= "return validate_form()" class = "login-table" action= "${pageContext.request.contextPath }/RegistServlet" method= "post" > <div class = "login-left" > <label class = "username" >用户名    </label> <input type= "text" id= "username" class = "yhmiput" name= "username" > </div> <div class = "login-left" > <label class = "username" >密码       </label> <input type= "password" id= "password" class = "yhmiput" name= "password" > </div> <div class = "login-left" > <label class = "username" >确认密码</label> <input type= "password" id= "repassword" class = "yhmiput" name= "repassword" > </div> <div class = "login-left" > <label class = "username" >上传头像</label> <input type= "file" class = "yhmiput" name= "file" > </div> <%-- <div class = "login-left" > <label class = "username" >验证码    </label> <input type= "text" class = "codeiput" name= "checkCode" > <img id= "codeImg" onclick= "changeImg()" src= "${pageContext.request.contextPath }/CheckImgServlet" > </div> --%> <div class = "login-btn" ><button>注册</button></div> </form> </div> </div> </div> </div> </div> <div class = "footer" > <p><span>M-GALLARY</span> © 2017 POWERED BY IMOOC.INC</p> </div> </body> </html> |
15
收起
正在回答 回答被采纳积分+1
1回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧