<body> <script type="text/javascript"> function yz() { if (form1.userName.value == "") { alert("用户名不能为空"); form1.userName.focus(); return form1(); } if (form1.userPassword.value == "") { alert("密码不能为空"); form1.userPassword.focus(); return form1(); } form1.registerid(); } </script> <form action="register" method="post" name="form1"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName" id="userName" placeholder="请输入用户名"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="userPassword" id="userPassword" placeholder="请输入密码"></td> </tr> <tr> <td><br></td> </tr> <tr> <td><input type="submit" name="registerid" value="注册" onClick="yz()"></td> </tr> </table> </form> </body> index.jsp
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String userName = request.getParameter("userName"); String userPassword = request.getParameter("userPassword"); if (userName=="" || userPassword=="") { System.out.println("注册失败"); response.sendRedirect("index.jsp"); }else{ System.out.println("注册成功"); System.out.println("用户名:" + userName + "密码:" + userPassword); request.getRequestDispatcher("login.jsp").forward(request, response); } out.flush(); out.close(); } register.java