JSP Page Redirect
When you need to move a document to a new location, you need to use JSP redirect.
The simplest way to redirect is to use the sendRedirect() method of the response object. The signature of this method is as follows:
public void response.sendRedirect(String location) throws IOException
This method sends the status code and the new page location back to the browser as a response. You can also use the setStatus() and setHeader() methods to achieve the same effect:
....
String site = "http://www.example.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
....
Example Demonstration
This example shows how JSP performs page redirect:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<html>
<html>
<head>
<title>页面重定向</title>
</head>
<body>
<h1>页面重定向</h1>
<%
// 重定向到新地址
String site = new String("http://www.example.com");
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
%>
</body>
</html>
Save the above code in a PageRedirecting.jsp file, then visit http://localhost:8080/PageRedirect.jsp, which will take you tohttp://www.example.com/。
Other Extensions