Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Page Redirection

JSP Page Redirection

When a document needs to be moved to a new location, JSP redirection is required.

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.runoob.com" ;

response.setStatus(response.SC_MOVED_TEMPORARILY);

response.setHeader("Location", site); 

....


Example Demonstration

This example shows how to perform page redirection in JSP:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*" %>







页面重定向







页面重定向

<% // 重定向到新地址 String site = new String("http://www.runoob.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %>

Save the above code in the PageRedirecting.jsp file, then visit http://localhost:8080/PageRedirect.jsp, which will take you tohttp://www.runoob.com/。