Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP page redirection


May 12, 2021 JSP


Table of contents


JSP page redirect

When you need to move the 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. Y ou can also use the setStatus() and setHeader() methods to get the same effect:

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

Example demonstration

This example shows how JSP redirects pages:

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Page Redirection</title>
</head>
<body>
<center>
<h1>Page Redirection</h1>
</center>
<%    
// 重定向到新地址    
String site = new String("http://www.w3cschool.cn");    
response.setStatus(response.SC_MOVED_TEMPORARILY);    
response.setHeader("Location", site);  %>
</body>
</html>

Save the above code in the PageRedirecting .jsp file, and then access http://localhost:8080/PageRedirect.jsp, which will take you to //www.w3cschool.cn/.