Typically, in a well-designed web application, 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.