About Servlet/JSP server redirection

Source: Internet
Author: User

There are two types of redirection technology: client redirection and server redirection. Client redirection can be achieved by setting specific HTTP headers or writing JavaScript scripts. This article mainly discusses the implementation of server-side redirection technology.

Redirection class on JSP server

The redirection technology on the JSP server involves several interfaces, including javax. servlet. ServletContext, javax. servlet. RequestDispatcher, javax. servlet. http. ServletRequest, javax. servlet. http. ServletResponse, and so on.

JSP server redirection

There are two methods to redirect the JSP server. One is to use the sendRedirect () method of HttpServletResponse, and the other is to use the forward () method of RequestDispatcher. These two methods are described below.

HttpServletResponse. sendRedirect () method

The HttpServletResponse interface defines the sendRedirect () method that can be used for redirection. The Code is as follows:

Public void sendRedirect (java. lang. String location) throws java. io. IOException

This method directs the response to the new URL specified by the parameter location. Location can be an absolute URL, such as response. sendRedirect ("http://java.sun.com") or a relative URL. If the location starts with "/", the container considers it relative to the root of the current Web application. Otherwise, the container will resolve it to the URL relative to the current request. This redirection method causes the client browser to jump to the request URL. The new URL is displayed in the address bar of the browser, which is similar to the above setting of HTTP response header information.

RequestDispatcher. forward () method

RequestDispatcher is a Web resource package that can be used to pass the current request to the resource or include the new resource to the current response. The RequestDispatcher interface defines two methods. See the following code:

Public interface RequestDispatcher {void forward (ServletRequest request, ServletResponse response); void include (ServletRequest request, ServletResponse response );}

The forward () method redirects the current request and response to the resource specified by the RequestDispacher. This is widely used in actual projects, because it is often necessary to complete a business operation across multiple steps. After each step completes the corresponding processing, it is switched to the next step. For example, business processing is usually processed in Servlet, and the processing result is directed to a JSP page for display. This looks similar to the Servlet chain function, but there are some differences. A RequestDispatcher object can send requests to any server resource, not just another Servlet. The include () method will include the output of the Request Dispatcher resource to the current output.

Note: The forward () method can be called only when the response is not output to the client. If the page cache is not empty, the cache is automatically cleared before redirection. Otherwise, an IllegalStateException is thrown.

How to obtain RequestDispatcher

There are three ways to get the Request Dispatcher object.

1. The getRequestDispatcher (String path) method of javax. servlet. ServletRequest. The path can be a relative path, but the current Servlet context cannot be exceeded. If path starts with "/", it is parsed as the root of the current context.

2. The getRequestDispatcher (String path) method of javax. servlet. ServletContext. The path must start with "/" and be relative to the current Servlet context. You can call the getContext (String uripath) of ServletContext to get another Servlet context and switch to a server resource link in the external context.

3. Use getNamedDispatcher (String name) of javax. servlet. ServletContext to get a Web Resource named name, including Servlet and JSP pages. The Resource Name is specified in the Web Application Deployment description file web. xml.

There are slight differences in the use of these three methods. For example, the following is an application configuration file web. xml:

 
 
  1. <?xml version="1.0" ?>
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app>
  3. <servlet><servlet-name>FirstServlet</servlet-name>
  4. <servlet-class>org. javaresearch.redirecttest.ServletOne</servlet-class></servlet>
  5. <servlet><servlet-name>SecondServlet</servlet-name>
  6. <servlet-class>org.javaresearch. redirecttest.ServletTwo</servlet-class></servlet>
  7. <servlet-mapping><servlet-name>FirstServlet</servlet-name>
  8. <url-pattern>/servlet/firstservlet/</url-pattern></servlet-mapping>
  9. <servlet-mapping><servlet-name>SecondServlet</servlet-name>
  10. <url-pattern>/servlet/secondservlet/</url-pattern></servlet-mapping></web-app> 

Two servlets are defined, named FirstServlet and SecondServlet respectively. The corresponding classes are org. javaresearch. redirecttest. ServletOne and org. javaresearch. redirecttest. ServletTwo. You can access through a link similar to the following in a browser:

Http: // localhost: 8080/servlet/firstservlet/

Use method 1. For example, you can write the following code in firstservlet:

 
 
  1. RequestDispatcher rd = request.getRequestDispatcher("secondservlet");rd.forward(request, response); 

Now the control is directed to the second Servlet.

The RequestDispatcher code in the Servlet Context is as follows:

 
 
  1. RequestDispatcher rd = getServletContext().getRequest Dispatcher("/servlet/secondservlet");rd.forward(request, response); 

The method in step 3 is used. From the preceding web. xml configuration file, we can see that two servlets are defined, named FirstServlet and SecondServlet, respectively, so we can get the named Dispatcher:

 
 
  1. RequestDispatcher rd = getServletContext().getNamedDispatcher("SecondServlet");rd.forward(request, response); 

In this way, you can also redirect to SecondServlet.

Redirection in JSP page

JSP is compiled to run as a Servlet after parsing, so the above redirection code can also be used in JSP, and JSP also provides more convenient operations, as shown below:

 
 
  1. <jsp:forward page= "nextpage.jsp"/> 

When the JSP page is executed here, the current processing will be terminated and the control will be handed over to nextpage. jsp.

How to select a JSP server

RequestDispatcher. forward () method and HttpServletResponse. the sendRedirect () method has the following differences: the former is only the redirection of control in the container, and the address after the redirection is not displayed in the address bar of the client browser; the latter is a complete redirection, the browser will get the jump address and resend the request link. In this way, the link address after the jump is displayed in the address bar of the browser. Therefore, the former is more efficient. When the former can meet the needs, try to use the Request Dispatcher. forward () method, and this will also help to hide the actual link. In some cases, for example, to jump to a resource on another server, you must use the HttpServletResponse. sendRequest () method.

  1. Get database connection in JSP
  2. Introduction to JSP Action
  3. Simplified code in JSP expressions
  4. Detailed explanation of JSP-to-Servlet Conversion
  5. Introduction to JSP Elements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.