servlet/jsp Server-side redirection

Source: Internet
Author: User
Tags header include relative resource client
Js|servlet| server Typically, in a well-designed Web application, the servlet and JSP technologies are used synthetically. The servlet controls the flow of business, and the JSP is responsible for the display of the results of the business process. In this case, the redirection technology will be used heavily.

Redirect technology can be divided into two categories, one is client redirect, the other is server side orientation. Client redirection can be done by setting a specific HTTP header, or by writing a JavaScript script. This paper mainly discusses the implementation of server side orientation technology.


Server-side redirection related classes


Server-side redirection technology involves Javax.servlet.ServletContext, Javax.servlet.RequestDispatcher, Javax.servlet.http.ServletRequest, Javax.servlet.http.ServletResponse and so on several interfaces.


Server-side redirection


There are two ways to redirect server-side, one is to use the HttpServletResponse Sendredirect () method, and the other is to use the RequestDispatcher forward () method. The following are an introduction to both of these approaches.

Httpservletresponse.sendredirect () method

The HttpServletResponse interface defines the Sendredirect () method that can be used for steering. The code is as follows:

public void Sendredirect (java.lang.String location) throws Java.io.IOException


This method directs the response to the specified, new URL of the parameter location. Location can be an absolute URL, such as Response.sendredirect ("http://java.sun.com"), or you can use a relative URL. If location begins with "/", the container considers the root of the current Web application to be the same as the container, otherwise, it resolves to a URL relative to the current request. This redirection method will cause the request URL of the client browser to jump. The new URL address can be seen from the address bar in the browser, which is similar to the implementation of the HTTP response header information set above.

Requestdispatcher.forward () method

RequestDispatcher is a wrapper for a web resource that can be used to pass the current request to that resource, or to include new resources in the current response. There are two methods defined in the RequestDispatcher interface, 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 used heavily in the actual project, because the completion of a business operation often involves a number of steps, each of which completes the appropriate processing and then moves to the next step. For example, business processes are typically handled in a servlet, and the results of processing are shifted to a JSP page for display. This looks similar to the functionality of the servlet chain, but there are some differences. A RequestDispatcher object can send a request to any server resource, not just another servlet. The include () method will include the output of the request dispatcher resource into the current output.

Note that the forward () method can be invoked only if the response has not been output to the client, and the cache is automatically cleared before redirection if the page cache is not empty. Otherwise, a IllegalStateException exception is thrown.


How to get RequestDispatcher


There are three ways to get the request dispatcher object.

1.javax.servlet. ServletRequest's Getrequestdispatcher (String path) method where path can be a relative path, but not the current servlet context. If path begins with "/", it resolves to a root relative to the current context.

2.javax.servlet. ServletContext's Getrequestdispatcher (String path) method where path must start with "/" and the path is relative to the current servlet context. You can invoke the ServletContext getcontext (String uripath) to get another servlet context and move to a server resource link to the external context.

3. Use Javax.servlet. ServletContext's Getnameddispatcher (String name) gets a Web resource named name, including Servlet and JSP pages. The name of this resource is specified in the Web application Deployment description file Web.xml.

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

<?xml version= "1.0"?>
<! 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>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>org. javaresearch.redirecttest.servletone</servlet-class>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>org.javaresearch. Redirecttest. Servlettwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet/firstservlet/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/servlet/secondservlet/</url-pattern>
</servlet-mapping>
</web-app>



It defines two servlet names, Firstservlet and Secondservlet respectively, and the corresponding classes are org.javaresearch. Redirecttest. Servletone and Org. Javaresearch.redirecttest.ServletTwo. You can access it in a browser by using a link similar to the following:

http://localhost:8080/servlet/firstservlet/

Use the method in 1, for example, in Firstservlet to write the following code:

RequestDispatcher rd = Request.getrequestdispatcher ("Secondservlet");
Rd.forward (request, response);



The control will now turn to the second servlet.

Using the method in 2, you can get the RequestDispatcher code from the servlet context as follows:

RequestDispatcher rd = Getservletcontext (). getrequest
Dispatcher ("/servlet/secondservlet");
Rd.forward (request, response);



Use the method in 3 from the top of the web. The XML configuration file can see the definition of two servlet, named Firstservlet and Secondservlet, so you can get the named dispatcher:

RequestDispatcher rd = Getservletcontext (). Getnameddispatcher ("Secondservlet");
Rd.forward (request, response);



This can also be redirected to the Secondservlet.


Redirects in a JSP page


JSP is compiled into a servlet to run after parsing, so you can also use the redirect code above in the JSP, and JSP also provides more convenient operations, as follows:

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



The JSP page executes here, terminating the current processing and handing control over to nextpage.jsp.


How to choose


The difference between the Requestdispatcher.forward () method and the Httpservletresponse.sendredirect () method is that the former is only a control shift in the container, and the address bar of the client browser does not show the turned The latter is a complete jump, the browser will get the address of the jump, and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser. So, the former is more efficient, when the former can meet the needs, try to use the request Dispatcher.forward () method, and it also helps to hide the actual link. In some cases, for example, you need to jump to a resource on a different server, you must use the Httpservletresponse.sendrequest () method.



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.