请求重定向:客户端行为,reponse.sendRedirect(),从本质上讲等同于两次请求,并且前一次的请求不会保存,地址栏的URL地址会改变。
请求转发:服务器行为,request.getRequestDispatcher().foward(request,response),是一次请求,转发后,前一次的请求对象会保存,地址栏的URL不会改变。
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录界面</title>
</head>
<body>
<h1>登录界面</h1>
<form action="goto.jsp" method="post">
用户名:<input type="text" name="username"><br><br>
密码:<input type="password" name="password"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
goto.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>跳转页面</title>
</head>
<body>
<%
//请求重定向 response.sendRedirect("user.jsp");
//请求转发 request.getRequestDispatcher("user.jsp").forward(request, response);
%>
</body>
</html>
user.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户页面</title>
</head>
<body>
<h1>用户信息</h1>
<%
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
%>
用户名:<%=username %><br><br>
密码:<%=password %>
</body>
</html>