美文网首页
servlet的两种转发

servlet的两种转发

作者: b099e4f1c471 | 来源:发表于2018-07-03 13:37 被阅读0次
    request response
    request.sendRedirect() response.getRequrstDispatcher().forword(request,response)
    共发两次请求 转发第一次的请求,可带参数
    地址栏会变 地址栏不变

    eg:
    login.jsp

    <form action="../LoginServlet" method="get">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        <input type="reset" value="重置">
        <input type="submit" value="提交">
    </form>
    

    LoginServlet.java(doGet()):

    HttpSession loginsession = request.getSession();
            //String name = (String)loginsession.getAttribute("username1");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if(username.length() >= 6) {
                while(password.equals("1234")) {
    //              response.sendRedirect("9(important)/success.jsp");
                    request.getRequestDispatcher("9(important)/success.jsp").forward(request, response);
                }
    //          response.sendRedirect("9(important)/login.jsp");
                request.getRequestDispatcher("9(important)/login.jsp").forward(request, response);
            }
            response.sendRedirect("9(important)/login.jsp");
    //      request.getRequestDispatcher("9(important)/login.jsp").forward(request, response);
            
            loginsession.invalidate();
    

    使用第1、2个注释的代码段的时候:
    报错:


    image.png

    相关文章

      网友评论

          本文标题:servlet的两种转发

          本文链接:https://www.haomeiwen.com/subject/neiduftx.html