WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP page redirection


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

The easiest 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 get the same effect:

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

Instance Demo

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>Page redirection</title>
</head>
<body>

<h1>Page redirection</h1>

<%
   // Redirect to new address
   String site = new String("http://www.welookups.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>

</body>
</html>