Web container invoke filter and servlet sequence learning

Source: Internet
Author: User

Web container invoke filter and servlet sequence learning
 has been confused about where the filter and servlet were called by the Web container, and then looked at the Tomcat source to uncover its veil.
1. Here is a simple timing diagram:



2. Analysis of the main classes used in the sequence diagram above

1) Applicationfilterchain class, there are two main functions, the following is the omitted code

Public voidDoFilter (request, Response) {//exposed outside the calling interface

if (globals.is_security_enabled) {

Final ServletRequest req = Request;

Final Servletresponse res = response;

Internaldofilter (Req,res);

return null;

} else {

Internaldofilter (Request,response);

}

}

Private voidInternaldofilter (request, Response) {

if (POS < N) {//Determine if there is a filter to execute

Applicationfilterconfig filterconfig = filters[pos++];

Filter filter = NULL;

Filter = Filterconfig.getfilter ();

Filter.dofilter (Request, response, this);

return;

}

{After//filter executes, the servlet is executed

if (Request instanceofhttpservletrequest) &&

(response instanceof httpservletresponse)) {

Servlet.service (httpservletrequest) request,

(httpservletresponse) response);

}

}


void addfilter (applicationfilterconfig filterconfig) {

if (n = = filters.length) {

applicationfilterconfig[] Newfilters =

New Applicationfilterconfig[n + INCREMENT];

System. arraycopy (filters, 0, newfilters, 0, N);

filters = newfilters;

}

filters[n++] = Filterconfig;

}

2) The main method of the Servlet class, take the HttpServlet class as an example, the main method is the service (request,response)

Public voidService (servletrequest req, servletresponse Res)

throws Servletexception, IOException {

HttpServletRequest request;

HttpServletResponse response;

Try {

Request = (httpservletrequest) req;

Response = (httpservletresponse) res;

} catch (ClassCastException e) {

Throw New Servletexception ("Non-http Request or response");

}

Service (request, response);//Internal method

}

protected voidService (httpservletrequest, HttpServletResponse)

throws Servletexception, IOException {

String method = Req.getmethod ();

if (Method.equals (method_get)) {

long lastmodified = getlastmodified (req);

if (LastModified = =-1) {

Doget (req, resp); Common methods

} Else {

long ifmodifiedsince;

Try {

Ifmodifiedsince = Req.getdateheader (header_ifmodsince);

} Catch (IllegalArgumentException iae) {

Ifmodifiedsince =-1;

}

if (Ifmodifiedsince < (lastmodified/1000 * 1000)) {

Maybesetlastmodified (resp, lastmodified);

Doget (req, resp);

} Else {

Resp.setstatus (HttpServletResponse. Sc_not_modified);

}

}

} Else if (method.equals (method_head)) {

long lastmodified = getlastmodified (req);

Maybesetlastmodified (resp, lastmodified);

Dohead (req, resp);

} Else if (method.equals (method_post)) {

DoPost (req, resp);//Common methods

} Else if (method.equals (method_put)) {

DoPut (req, resp);

} Else if (method.equals (method_delete)) {

DoDelete (req, resp);

} Else if (method.equals (method_options)) {

Dooptions (REQ,RESP);

} Else if (method.equals (method_trace)) {

Dotrace (REQ,RESP);

} Else {

String errmsg =lstrings. getString ("http.method_not_implemented");

object[] Errargs = new object[1];

Errargs[0] = method;

ErrMsg = Messageformat. format (ErrMsg, Errargs);

Resp.senderror (HttpServletResponse. sc_not_implemented, errmsg);

Web container invoke filter and servlet sequence learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.