Redirection (Z) of ServletJSP Server

xiaoxiao2021-03-06  75

Typically, in a well-designed Web application, servlet and JSP technology are used. The servlet control service is transferred, and JSP is responsible for the display of business processing results. At this point, a large number of redirect technologies will be used. Redirecting technology can be divided into two categories, and one is the client redirect, and one is the server-side redirection. The client redirect can be implemented by setting a specific HTTP header or writing a JavaScript script. This paper mainly explores the realization of server-side redirectional technology.

Return of server-side related classes

Server-side redirective technology involves javax.servlet.servletContext, javax.servlet.RequestDispatcher, javax.servlet.http.servletRequest, javax.servlet.http.servletResponse, etc. Several interfaces.

Server-side redirection mode

There are two ways to redirect the server side. First, use the HTTPServletResponse's sendRedirect () method, one is the Forward () method using the RequestDispatcher. Below the two ways. The httpservletResponse.sendRedirect () method The HTTPServletResponse interface defines the sendredirect () method that can be used for steering. code show as below:

Public void sendirect (java.lang.string location) THROWS JAVA.IO.IEXCEPTION

This method will definite to the parameter location specified, the new URL. Location can be an absolute URL, such as response.sendredirect ("http://java.sun.com") or relatively URL. If Location begins with "/", the container considers the root relative to the current web application, otherwise, the container will resolve the URL relative to the current request. This redirection will lead to a request URL of the client browser. From the address bar in your browser, you can see the new URL address, which is similar to the implementation of the HTTP response header information above. RequestDispatcher.Forward () method RequestDispatcher is a wrapper of a web resource that can be used to pass current Request to the resource, or put new resources into the current response. Two methods are 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 in a large number of actual projects, because completing a business operation often requires spanning multiple steps, each step is completed, turned to the next step. For example, the usual service processing is processed in the servlet, and the result of the process turns 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 the request 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 that the Forward () method can only be called if the response has not yet been output to the client, if the page cache is not empty, the cache will be automatically cleared before the page. Otherwise an IllegalStateException will throw an exception. 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 cannot overcome the current servlet context. If the PATH starts with "/", resolve the root relative to the current context. 2. Javax.servlet. ServletContext's getRequestDispatcher (String path) method, where Path must begin with "/", the path relative to the current servlet context. You can call the getContext (String Uripath) of ServletContext to get another servlet context and can be turned to a server resource link to the external context. 3. Use javax.servlet. ServletContext GetNamedDispatcher (String Name) Get 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. The use of these three methods has a subtle difference. For example, the following is an application configuration file Web.xml:

"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

firstservlet

org. javaresearch.redirecttest.servletone

secondServlet

org.javaresearch. redirectTest.Servlettwo

firstservlet

/ servlet / firstservlet /

secondServlet

/ servlet / secondservlet /

Among them, two servlets are defined, named, respectively, corresponding classes, respectively, org.javaresearch. RedirectTest.Servletone and Org. JavaResearch.RedirectTest.Servlettwo. Can be accessed in the browser to access the following link: http: // localhost: 8080 / servlet / firstservlet / use 1 method, for example, in the firstservlet, you can write the following code:

RequestDispatcher Rd = Request.getRequestDispatcher ("SecondServlet");

Rd.Forward (Request, Response);

At this point, the control will turn to the second servlet. Using the method in 2, you can get the requestDispatcher code from servlet context as follows:

RequestDispatcher rd = getServletContext (). GetRequest

Dispatcher ("/ servlet / secondservlet);

Rd.Forward (Request, Response);

Using the 3 method, from the above web. XML configuration file can see the two servlets defined, the names are FirstServlet and SecondServlet, so you can get named Dispatcher:

RequestDispatcher rd = getServletContext (). Getnameddispatcher ("secondservlet");

Rd.Forward (Request, Response);

This can also be redirected to SecondServlet.

Redirection in the JSP page

JSP compiles to a servlet after parsing, so you can also use the above redirection code in JSP, and JSP provides more convenient operation, as follows:

The JSP page is executed here, and the current processing will be terminated, and the control will be paid to NextPage.jsp.

how to choose

The part of the requestdispatcher.forward () method and httpservletResponse.sendredirect () method is that the former is only the steering of the control in the container, and does not show the turn-back address in the client browser address bar; the latter is complete Jump, the browser will get the address of the jump and resend the request link. Thus, the link address after the jump is seen from the address bar of the browser. Therefore, the former is more efficient, and when the former can meet the needs, try to use the Request Dispatcher.Forward () method, and this also helps hide the actual link. In some cases, for example, you need to jump into a resource on a further server, you must use the httpservletResponse.sendRequest () method.

转载请注明原文地址:https://www.9cbs.com/read-121554.html

New Post(0)