Analysis of service methods in the servlet life cycle

Source: Internet
Author: User

Problem
Within the service method in Servletlifecycle, there is super.service (request, response); Will execute This.doget (httpservletrequest request, httpservletresponse response);
No Super.service (request, response); This.doget (...) is not executed. How did it come true?

Extrapolate
A subclass, in a method of overwrite, if the method of the parent class is called, another method within the subclass is executed;
In the Overwrite method, if the method of the parent class is not called, another method within the subclass is not executed;

Analysis-----> Symbols are key notes

1  Public classServletlifecycleextendsHttpServlet {2     Private Static Final LongSerialversionuid = 1L;3     4 @Override5      Public voidService (ServletRequest request, servletresponse response)throwsservletexception, IOException {6         //TODO auto-generated Method Stub7         //This.doget (httpservletrequest) request, (HttpServletResponse) response);8         Super. Service (Request, response);//-------------------> Execute the Service (ServletRequest request, Servletresponse Response) method of the parent class9          TenSYSTEM.OUT.PRINTLN ("Processing client requests"); One     } A  -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -         //TODO auto-generated Method Stub theSYSTEM.OUT.PRINTLN ("Process"); -Response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); -     } -  + } -  +  A  at  -  Public Abstract classHttpServletextendsGenericservlet { -  Public voidService (ServletRequest req, servletresponse Res) -     throwsservletexception, IOException { -  - httpservletrequest request; in httpservletresponse response; -  to     Try { +Request =(httpservletrequest) req; -Response =(HttpServletResponse) res; the}Catch(classcastexception e) { *         Throw NewServletexception ("Non-http Request or response"); $     }Panax NotoginsengService (request, response);//-------------------> Overloads of the Parent class's methods perform the service of the parent class (HttpServletRequest request, HttpServletResponse response) method  -     //-------------------> My understanding is that if there are no overloads, there will be a dead loop. Go here and execute the service method of the subclass Servletlifecycle, and the subclass calls the parent class service method, looping the nesting. the } +  A  the protected voidService (HttpServletRequest req, HttpServletResponse resp) +         throwsservletexception, IOException { -  $String method =Req.getmethod (); $  -         if(Method.equals (method_get)) { -             LongLastModified =getlastmodified (req); the             if(LastModified = =-1) { -                 //servlet doesn ' t support if-modified-since, no reasonWuyi                 //To go through further expensive logic theDoget (req, resp);//-------------------> Call doget method of subclass Servletlifecycle -}Else { Wu                 Longifmodifiedsince; -                 Try { AboutIfmodifiedsince =Req.getdateheader (header_ifmodsince); $}Catch(illegalargumentexception iae) { -                     //Invalid Date Header-proceed as if none was set -Ifmodifiedsince =-1; -                 } A                 if(Ifmodifiedsince < (lastmodified/1000 * 1000)) { +                     //If The servlet mod time is later, call Doget () the                     //Round down to the nearest second for a proper compare -                     //A ifmodifiedsince of-1 would always is less $ maybesetlastmodified (resp, lastmodified); the doget (req, resp); the}Else { the Resp.setstatus (httpservletresponse.sc_not_modified); the                 } -             } in  the}Else if(Method.equals (Method_head)) { the             LongLastModified =getlastmodified (req); About maybesetlastmodified (resp, lastmodified); the Dohead (req, resp); the  the}Else if(Method.equals (method_post)) { + doPost (req, resp); -  the}Else if(Method.equals (method_put)) {Bayi DoPut (req, resp); the  the}Else if(Method.equals (Method_delete)) { - DoDelete (req, resp); -  the}Else if(Method.equals (method_options)) { the dooptions (REQ,RESP); the  the}Else if(Method.equals (method_trace)) { - Dotrace (REQ,RESP); the  the}Else { the             //94             //Note that this means NO servlet supports whatever the             //method is requested, anywhere on the this server. the             // the 98String errmsg = lstrings.getstring ("http.method_not_implemented"); Aboutobject[] Errargs =NewObject[1]; -Errargs[0] =method;101ErrMsg =Messageformat.format (errmsg, Errargs);102 103 Resp.senderror (httpservletresponse.sc_not_implemented, errmsg);104         } the     }106 107 108 protected voiddoget (httpservletrequest req, HttpServletResponse resp)109     throwsservletexception, IOException the {111String protocol =Req.getprotocol (); theString msg = lstrings.getstring ("http.method_get_not_supported");113     if(Protocol.endswith ("1.1")) { the Resp.senderror (httpservletresponse.sc_method_not_allowed, msg); the}Else { the Resp.senderror (Httpservletresponse.sc_bad_request, msg);117     }118 }119}

Analysis of service methods in the servlet life cycle

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.