重定向与页面转发
- 参考网址
- 参考网址
- 转发地址栏没有变化;重定向地址栏有变化;
- 转发的是同一次请求;重定向是两次不同请求
- 转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
- 重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
demo
chongdingxiang.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试</title>
<script type="text/javascript" src="js/jquery.js">></script>
<script type="text/javascript" >
function chongdingxiang1() {
var url = "Chongdingxiang_test?name=" + $("#name").val();
window.open(url);
}
function chongdingxiang2() {
var url = "Chongdingxiang_test2?name=" + $("#name").val();
window.open(url);
}
</script>
</head>
<body>
请输入姓名:<br/>
<input type="text" id="name" />
<input type="submit" value="提交1" onclick="chongdingxiang1()" />
<input type="submit" value="提交2" onclick="chongdingxiang2()" />
</body>
</html>
chongdingxiang_test.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Chongdingxiang_test
*/
@WebServlet("/Chongdingxiang_test")
public class Chongdingxiang_test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Chongdingxiang_test() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
//String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String name=request.getParameter("name");
System.out.println(name+"_11111111");
//重定向
response.sendRedirect("test.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
chongdingxiang_test2.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Chongdingxiang_test2
*/
@WebServlet("/Chongdingxiang_test2")
public class Chongdingxiang_test2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Chongdingxiang_test2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
//String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String name=request.getParameter("name");
System.out.println(name+"_222222222");
//页面转发
request.getRequestDispatcher("test.html").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}