English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP Page Redirection

When you need to move a document to a new location, you need to use JSP redirection.

The simplest way to perform a 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 the response. You can also use the setStatus() and setHeader() methods to achieve the same effect:

....
String site = "http://www.oldtoolbag.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
....

Example Demonstration

This example demonstrates 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>Page Redirection</title>
</head>
<body>
<h1>Page Redirection</h1>
<%
   // Redirect to a new address
   String site = new String("http://www.oldtoolbag.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>
</body>
</html>

Save the above code in the PageRedirecting.jsp file, then access http://localhost:8080/PageRedirect.jsp, it will redirect you tohttp://www.oldtoolbag.com/.