Latest web development tutorials

JSP page redirects

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

The easiest way is to use the redirect response object sendRedirect () method. Signature of this method are as follows:

public void response.sendRedirect(String location)
throws IOException 

This method will state code and the new page location as a response back to the browser. You can also use setStatus () and setHeader () method to get the same effect:

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

Examples Demo

This example shows how a JSP page redirects:

<%@ 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.w3big.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>

</body>
</html>

PageRedirecting.jsp code above is saved in a file, and then visit http: // localhost: 8080 / PageRedirect.jsp , it will take you to http://www.w3big.cc/ .