如何理解response对象的转发和重定向
程序员文章站
2022-04-20 22:21:05
...
用代码理解response对象的转发和重定向
register页面代码
<body>
<%
// 接收传递过来的值
String price = (String) request.getAttribute("price");
if (price == null)
price = "";
%>
<form action="changeOver.jsp" method="post">
用户名:<input type="text" name="userName" /> <br/>
密码:<input type="password" name="pwd" />
<span style="color:red"><%=price%></span> <br/>
<input type="submit" value="登录" />
</form>
</body>
changeOver页面代码
<body>
<%
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if("admin".equals(userName) && "1234".equals(pwd)){
response.sendRedirect("failing.jsp");
}else{
request.setAttribute("price","密码错误");
request.getRequestDispatcher("register.jsp").forward(request,response);
}
%>
</body>
failing页面代码
<body>
<h1>登录成功!</h1>
</body>