老师,我的这个哪里处理问题
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 | @WebServlet ( "/RgServlet" ) public class RgServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RgServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username =request.getParameter( "username" ); String password =request.getParameter( "password" ); String phone =request.getParameter( "phone" ); String email =request.getParameter( "email" ); /*对获取的参数进行校验,校验方法为matches*/ //用户名只能是字母,且长度为6-12位 String usernameRegex = "[a-zA-Z]{6,12}"; boolean flag1 = username.matches(usernameRegex); //密码只能是数字,且长度为6位 String passwordRegex = "[0-9]{6,}"; boolean flag2 = password.matches(passwordRegex); //手机号校验 String phoneRegex = "[1][3578][0-9]{9}"; boolean flag3 = phone.matches(phoneRegex); /* * 邮箱校验 * [a-zA-Z_0-9]{3,}@([a-zA-Z]+|\\d+)(\\.[a-zA-Z]+)+ * +表示该位置前面字符的出现次数必须大于或等于1次 |表示或 */ String emailRegex = "[a-zA-Z_0-9]{3,}@([a-zA-Z]+|\\d+)(\\.[a-zA-Z]+)+" ; boolean flag4 = email.matches(emailRegex); //如果username,password,phone,email同时满足要求才会打印 if (flag1 && flag2 && flag3 && flag4) { System.out.println( "uasername:" + username); System.out.println( "password:" + password); System.out.println( "phone:" + phone); System.out.println( "email:" + email); } else { System.out.println( "输入的数据不符合格式要求" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super .doPost(req, resp); } } |
jsp里表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <form action= "/Rg/rg/RgServlet" method= "post" > <p> 用户名:<input type= "text" name= "username" pattern= "[a-zA-Z]{6,12}" required= "required" placeholder= "请输入6-12位的字母" > </p> <p> 密 码:<input type= "password" name= "password" pattern= "[0-9]{6,}" required= "required" placeholder= "请输入至少6位数字" > </p> <p> 手机号:<input type= "text" name= "phone" pattern= "1[3578]\d{9}" required= "required" placeholder= "请输入手机号" > </p> <p> 邮 箱:<input type= "email" name= "email" placeholder= "请输入邮箱" required= "required" > </p> <p> <input type= "submit" value= "注册" > <input type= "reset" > <input type= "reset" value= "重置" > </p> </form> |
然后点击提交后会报错404,我实在找不到为啥了
0
收起
正在回答
2回答
同学你好,同学已经可以自己调整代码了,很棒呐!
405错误是请求方式问题,在页面中使用post方式提交,但是在RgServlet中使用doGet方法接收数据,导致了405错误。
这里可以将数据的接收等内容放在doPost方法中,也可以修改doPost方法中的内容,在doPost中调用doGet方法,例如:
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 | @WebServlet ( "/RgServlet" ) public class RgServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RgServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username =request.getParameter( "username" ); String password =request.getParameter( "password" ); String phone =request.getParameter( "phone" ); String email =request.getParameter( "email" ); /*对获取的参数进行校验,校验方法为matches*/ //用户名只能是字母,且长度为6-12位 String usernameRegex = "[a-zA-Z]{6,12}"; boolean flag1 = username.matches(usernameRegex); //密码只能是数字,且长度为6位 String passwordRegex = "[0-9]{6,}"; boolean flag2 = password.matches(passwordRegex); //手机号校验 String phoneRegex = "[1][3578][0-9]{9}"; boolean flag3 = phone.matches(phoneRegex); /* * 邮箱校验 * [a-zA-Z_0-9]{3,}@([a-zA-Z]+|\\d+)(\\.[a-zA-Z]+)+ * +表示该位置前面字符的出现次数必须大于或等于1次 |表示或 */ String emailRegex = "[a-zA-Z_0-9]{3,}@([a-zA-Z]+|\\d+)(\\.[a-zA-Z]+)+" ; boolean flag4 = email.matches(emailRegex); //如果username,password,phone,email同时满足要求才会打印 if (flag1 && flag2 && flag3 && flag4) { System.out.println( "uasername:" + username); System.out.println( "password:" + password); System.out.println( "phone:" + phone); System.out.println( "email:" + email); } else { System.out.println( "输入的数据不符合格式要求" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // super.doPost(req, resp); doGet(request, response); //调用doGet方法 } } |
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧