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.
HttpservletResponse.sendRedirect () method
The HTTPSERVLETRESPONSE interface defines the SendRedirect () method that can be used for steering. code show as below:
Public void sendredirect (java.lang.string location) throws .io.ioException
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 to include 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);} forward () method to redirect the current request and response RequestDispacher to the specified resource. 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:
XML Version = "1.0"?>
Among them, two servlets are defined, named, respectively, corresponding classes, respectively, org.javaresearch. RedirectTest.Servletone and Org. JavaResearch.RedirectTest.Servlettwo. You can access the following link to the browser: http: // localhost: 8080 / servlet / firstservlet / use 1 method, for example, in the firstservlet: 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 (). GetRequestDispatcher ("/ 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 also provides more convenient operation, as follows: