2.17. Servlet web page redirection

发布时间 :2023-12-14 23:00:02 UTC      

When the document is moved to a new location and we need to send this new location to the client, we need to use web page redirection. Of course, it may also be for load balancing, or just for simple random, all of which may involve web redirection.

The easiest way to redirect a request to another web page is to use the response object’s sendRedirect() method. The following is the definition of the method:

public void HttpServletResponse.sendRedirect(String location)
throws IOException

This method sends the response back to the browser along with the status code and the new web page location. You can also do this by putting setStatus() and setHeader() method to achieve the same effect:

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

2.17.1. Example

This example shows how Servlet redirects the page to another location:

package com.runoob.test;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PageRedirect
 */
@WebServlet("/PageRedirect")
public class PageRedirect extends HttpServlet{

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html;charset=UTF-8");

      // New location to redirect
      String site = new String("http://www.runoob.com");

      response.setStatus(response.SC_MOVED_TEMPORARILY);
      response.setHeader("Location", site);
    }
}

Now let’s compile the Servlet above, and in the web.xml create the following entries in the file:

....
 <servlet>
     <servlet-name>PageRedirect</servlet-name>
     <servlet-class>PageRedirect</servlet-class>
 </servlet>

 <servlet-mapping>
     <servlet-name>PageRedirect</servlet-name>
     <url-pattern>/TomcatTest/PageRedirect</url-pattern>
 </servlet-mapping>
....

Now call this Servlet by accessing URL http://localhost:8080/PageRedirect . This will take you to the given URL http://www.runoob.com .

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.