Servlet/JSP server redirection

Source: Internet
Author: User
Generally, Servlet and JSP technologies are integrated in a well-designed web application. Servlet controls business flow, and JSP is responsible for displaying business processing results. At this time, the redirection technology will be widely used.

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 the server

Server-side redirection technology involves javax. servlet. servletcontext, javax. servlet. requestdispatcher, javax. servlet. http. servletrequest, javax. servlet. http. servletresponse, and other interfaces.

Server-side redirection

You can use either the sendredirect () method of httpservletresponse or 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:

<? 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>

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:

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:

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:

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:

<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

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.

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.