Servlet and Servlet, Servlet and JSP (essentially servlet) need to cooperate. It is mainly for the following reasons:
1. When web programs are complicated, it is unreasonable to use a servlet to complete all functions.
2. Different Web components are divided into different parts. For example, servlet is more suitable for controlling requests and responses, while JSP is more suitable for serving as interfaces in programs.
3. Different Web applications often need to call each other. Such mutual calls are only in the form of HTML hyperlinks.
Servlet cooperation with other Web components (such as servlet, JSP, and HTML) involves three main types: servlet Link (chaining), including response content (include), and forward request (forward ).
The newer Web application server does not support servlet links. Instead, it uses the filter link mechanism. The following two mechanisms are described: Response content and transfer request.
1. Include response content
As the name suggests, the principle of cooperation mechanism, including response content, is to include the response content of another servlet in one servlet content. Specifically, when the servlet needs to embed another web resource, it first establishes a Request dispatch object (requestdispatcher), and then uses the include method of the requestdispatcher object to include other Web Resources in the response, finally, combine the content of the original Servlet and the contained Web component into the response output of the original Servlet and send it to the client. Note that the contained Web component (sevlet or JSP) cannot change the status encoding or header information of the servletresponse object passed in from the original servlet. Otherwise, an exception is triggered. Refer to the following code:
1.
Public class includeservlet extends httpservlet {
Public void doget (httpservletrequest req, httpservletresponse resp)
{
Requestdispatcher dispatcher = Req. getrequestdispatcher ("/servletbeincluded ");
If (dispatcher! = NULL)
Dispatcher. Include (req, resp );
...
}
...
}
2.
Public class includeservlet extends httpservlet {
Public void doget (httpservletrequest req, httpservletresponse resp)
{
Requestdispatcher dispatcher = getservetconfig (). getservletcontext (). getrequestdispatcher ("/servletbeincluded ");
If (dispatcher! = NULL)
Dispatcher. Include (req, resp );
...
}
...
}
3.
Public class includeservlet extends httpservlet {
Public void doget (httpservletrequest req, httpservletresponse resp)
{
Requestdispatcher dispatcher = getservetconfig (). getservletcontext (). getcontext ("/webapp2"). getrequestdispatcher ("/servletbeincluded ");
If (dispatcher! = NULL)
Dispatcher. Include (req, resp );
...
}
...
}
The code in the preceding three sections illustrates three methods for obtaining requestdispatcher, the first code is to obtain another servlet dispatcher under the current web application from the current servletrequest object, that is, this line of code: req. getrequestdispatcher ("/servletbeincluded"); the second code is obtained by the context object of the Web application; the third code is to get the dispatcher of the servlet of another Web application from the context object of another web application. The three sections of Code illustrate the following:
1. The getrequestdispatcher () method of the current servletrequest object can only obtain the dispatcher of the Web component of the current web application, achieving collaboration between different Web Components in the same web application.
2. the dispatcher of the Web component of the current or other Web applications can be obtained by servletcontext, so that collaboration can be performed between different web applications.
Ii. transfer requests to other web resources
Similar to the include mechanism above, this collaboration mechanism also requires obtaining the resquestdispatcher of the web resource to be collaborated (servlet or JSP) first. Different from the include mechanism, the former is contained, while the latter is transmitted, that is, the response is directed to another Web component and processed by another Web component. That is to say, after the transfer is executed, the URL name path of the servletrequest object will change to the URL of the target Web component. Refer to the following code:
Public class includeservlet extends httpservlet {
Public void doget (httpservletrequest req, httpservletresponse resp)
{
Requestdispatcher dispatcher = Req. getrequestdispatcher ("/servletwillbeforward ");
If (dispatcher! = NULL)
Dispatcher. Forward (req, resp );
...
}
...
}
There is also a form of transfer request: httpservletrespneuron. sendredirect (string location ). This method is different from forward: sendredirect sends an http url redirection response to the client, allowing the client browser to load the current Web request to another URL. Therefore, the relative path of the sendredirect method parameter is not the current path relative to the current web application, but the root path relative to the Web application server; or you can directly specify the absolute path in the parameter, such:
Resp. setredirect ("http://xiya.com.cn/xyoa ").