JSP page redirection
When you need to move the document to a new location, you need to use JSP redirection.
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 new page position back to the browser as a response. You can also use the setStatus() and setHeader() methods to get the same effect:
.... String site = "http://www.php.cn" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....
Example demonstration
This example shows how JSP performs page redirection:
<%@ 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.php.cn"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>
Save the above code in the PageRedirecting.jsp file, then visit http://localhost:8080/PageRedirect.jsp, it will take you tohttp://www.php.cc/.