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
网友评论