Servlet API - RequestDispatcher interface brief description
Overview of the RequestDispatcher interface: Public Interface RequestDispatcher defines an object, which is responsible for receiving requests from the client on the server and sending resources to them (such as a servlet, html file, or JSP file). The servlet container creates a RequestDispatcher object that is used to package a server resource for a given location or name. This interface is designed in packaging servlets, but a servlet container can create this object to ensure any type of resource.
Forward () Method Overview: Public Void Forward (ServletRequest Request, ServletResponse response) throws servletexception, java.ioException advances a request to a request to a server (eg servlet, jsp file, or html file). This method allows a servlet to initially process a request, while another resource will respond. Since a RequestDispatcher object is obtained through the getRequestDispatcher () method, the ServletRequest object has its own path elements and variables to match the target resource path. The forward () method should be evoked before the response is submitted to the client (before the response main body output is refreshed). If the response has been submitted, the method throws IllegalStateException. Before Forward, the output that is not submitted will be automatically cleaned. The two parameters of the request and response must be the same object as the service () method passed to the servlet, or by the subclass of ServletRequestWrapper, ServletResponseWrapper (packaged).
Include () Method Overview: Public Void Include (ServletRequest Request, ServletResponse Response) Throws ServleTexception, Java.ioException This method contains content of certain resources in response (eg servlet, jsp file, html file). The key is the key to the server side. The ServletResponse object contains the remaining path elements and parameters from the caller. The servlet that is included cannot change the response status code or set header information; any attempt to make changes will be ignored. The two parameters of the request and response must be the same object as the service () method passed to the servlet, or by the subclass of ServletRequestWrapper, ServletResponseWrapper (packaged).
Finally, see servletContext.getRequestDispatcher (java.lang.string), servletcontext.getnameddispatcher (java.lang.string), servletRequest.GetRequestDispatcher (java.lang.string)
-------------------------------------------------- -------------------------------------------------- -----------------------------------
RequestDispatcher
Forward
1. If you have a parameter in the Forward URL, and the existing parameter in the Request object is the same name, the getParameter method returns the parameters in the Forward URL 2. Before Forward, the container should read the parameters in Post Form Data 3. If the Forward URL With jsessionID parameters, the container will make it as part of the URL, not sessionID4. Before the Forward method call, the container first clears the output cache, then throws IllegalStateException5. Before the Forward method returns The container should send all the data to the browser and turn off the output stream, which means that all the data output using the output stream will ignore 6. Before Forward, the container should reset the request below the Forward URL. Properties: servletpath, requesturi, queryString, pathinfo. RequestURL does not change, if it is obtained with getNameDispatcher, it is not necessary to reset the 7. Forward request, Welcome Files8. Forward should be processed, not filting9. Request and response parameters can be Implementation of containers, you can also servletRequestWrapper and ServletResponseWrapper or their subclass include
1. If there is a parameter in the include URL, and the existing parameter in the Request object is the same name, the getParameter method returns the parameter in the include URL 2. Before INCLUDE, the container should read the parameters in Post Form Data 3. If include URL With jsessionID parameters, the container will be part of the URL, not sessionID4. In the included servlet, only the output stream output, no response, all the settings are ignored, but do not leave 5. Before INCLUDE, the container does not reset the various attributes, but will contain contextpath, servletpath, pathingfo, querystring, requesturi in the ATTRIBUTE of Request (if you use getNameDispather, no)
Javax.Servlet.include.Context_path = contextPath javax.servlet.include.Servlet_path = servletpath javax.servlet.include.path_info = pathinfo javax.servlet.include.Query_String
-------------------------------------------------- -------------------------------------------------- ---------------------------------------
The referendum of the servlet / JSP server is unknown
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. The redirection of the server-side redirect-related class server-side refine technology involves javax.servlet.servletContext, javax.servlet.RequestDispatcher, javax.servlet.http.servletRequest, Javax.Servlet.http.ServletResponse, etc. Several interfaces. Figure 1 is a diagram of the relationship between the two interfaces. Figure 1 Redirects the resetting of the related class relationship diagram server-side redirects can have two ways, one is the use of 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. The code is as follows: public void sendirect (java.lang.string location) throws java.ioException This method will respond 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. RequestDispatcher interface defines two methods, 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 Go 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 has 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"?> (Web-APPPPP> "SERVLET "SERVLET-NAMEFIRSTSERVLET "nSERVLET-NAME>" SERVET-CLASSSS. JAVARESEARCH. RedirectTest.Servletone servlet-class> servlet>