老师,我的这个哪里处理问题

老师,我的这个哪里处理问题

@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);
	}
}

http://img1.sycdn.imooc.com//climg/5d9bf5000932d77611550306.jpg

jsp里表单

<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>
				密&nbsp;码:<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>
				邮&nbsp;箱:<input type="email" name="email" placeholder="请输入邮箱"
					required="required">
			</p>
			<p>
				<input type="submit" value="注册"> <input type="reset">
				<input type="reset" value="重置">
			</p>
		</form>

然后点击提交后会报错404,我实在找不到为啥了

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,同学已经可以自己调整代码了,很棒呐!

    405错误是请求方式问题,在页面中使用post方式提交,但是在RgServlet中使用doGet方法接收数据,导致了405错误。

这里可以将数据的接收等内容放在doPost方法中,也可以修改doPost方法中的内容,在doPost中调用doGet方法,例如:

@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方法
    }
}

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 随意看看 提问者 #1
    我明白了老师。我最开始也想到了这点所以手动继承了一下doPost方法,但是没修改参数,用的它默认的参数。
    2019-10-08 15:28:12
提问者 随意看看 2019-10-08 10:49:13

我知道原因了老师,URL地址给写错了- -!


但是又用新的问题,在form中的method属性不让我使用post方法进行提交,会报错405- -!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师