The wrapper of Tomcat learning

Source: Internet
Author: User

the wrapper of Tomcat learningCategory: Web server 2012-08-30 22:16 1547 people read reviews (0) favorite reports Tomcatservletwrapperservletslistexception

Wrapper represents a servlet that manages the loading, initialization, execution, and resource recycling of a servlet, including Servlets. Its parent container is generally context,wrapper is the lowest-level container, it has no child container, so the call to its addChild will be thrown illegalargumentexception. The Wrapper implementation class is Standardwrapper,standardwrapper also implements a servletconfig that has a servlet initialization information, which shows that Standardwrapper will directly and servlet To deal with all kinds of information.

When the Standardcontext is started, the Web. XML configuration file is read, and after the context is configured, some of the subordinate components of the context are launched, and those wrapper labeled "Load on Start" are loaded.

[Java]View Plaincopyprint?
    1. Load and initialize all "load on startup" Servlets
    2. if (OK) {
    3. Loadonstartup (Findchildren ());
    4. }
[Java]View Plaincopyprint?
  1. Public void Loadonstartup (Container children[]) {
  2. //Collect "load on startup" Servlets, need to be initialized
  3. Treemap<integer, arraylist<wrapper>> map =
  4. New Treemap<integer, arraylist<wrapper>> ();
  5. For (int i = 0; i < children.length; i++) {
  6. Wrapper Wrapper = (Wrapper) children[i];
  7. int loadonstartup = Wrapper.getloadonstartup ();
  8. if (Loadonstartup < 0)
  9. continue;
  10. Integer key = Integer.valueof (Loadonstartup);
  11. arraylist<wrapper> list = Map.get (key);
  12. if (list = = null) {
  13. List = new arraylist<wrapper> ();
  14. Map.put (key, list);
  15. }
  16. List.add (wrapper);
  17. }
  18. //Load the collected "load on startup" Servlets
  19. For (arraylist<wrapper> list:map.values ()) {
  20. For (Wrapper wrapper:list) {
  21. try {
  22. Wrapper.load ();
  23. } catch (Servletexception e) {
  24. GetLogger (). Error (Sm.getstring ("Standardwrapper.loadexception",
  25. GetName ()), Standardwrapper.getrootcause (e));
  26. //Note:load errors (including a servlet that throws
  27. //Unavailableexception from THT init () method) is not
  28. //Fatal to application startup
  29. }
  30. }
  31. }
  32. }

This method does two things:

1. Traverse these wrapper and put them in a map. Key is the boot order, value is the servlet list of the same boot order, in order to protect the small number of first start, here is used treemap this data structure to store;

2. Traverse the map and load each wrapper in the corresponding list in turn. Since the ArrayList is used, the same load on start value is loaded first

Let's take a look at the load method of Standardwrapper and call the Loadservlet method directly to initialize the [Java]View Plaincopyprint?
  1. Public synchronized Void load () throws servletexception {
  2. Instance = Loadservlet ();
  3. if (!instanceinitialized) {
  4. Initservlet (instance);
  5. }
  6. if (isjspservlet) {
  7. StringBuilder oname =
  8. New StringBuilder (Mbeanutils.getdomain (GetParent ()));
  9. Oname.append (": type=jspmonitor,name=");
  10. Oname.append (GetName ());
  11. Oname.append (Getwebmodulekeyproperties ());
  12. try {
  13. Jspmonitoron = New ObjectName (oname.tostring ());
  14. Registry.getregistry (null, null)
  15. . RegisterComponent (instance, Jspmonitoron, null);
  16. } catch (Exception ex) {
  17. Log.info ("Error registering JSP monitoring with JMX" +
  18. instance);
  19. }
  20. }
  21. }
[Java]View Plaincopyprint?
  1. private synchronized void Initservlet (servlet servlet)throws servletexception {
  2. if (instanceinitialized &&!singlethreadmodel) return;
  3. //Call the initialization method of this servlet
  4. try {
  5. Instancesupport.fireinstanceevent (Instanceevent.before_init_event, servlet);
  6. if (globals.is_security_enabled) {
  7. object[] args = new object[] {(facade)};
  8. Securityutil.doasprivilege ("Init", servlet, ClassType, args);
  9. args = null;
  10. } Else {
  11. Servlet.init (facade);
  12. }
  13. instanceinitialized = true;
  14. Instancesupport.fireinstanceevent (Instanceevent.after_init_event, servlet);
  15. } catch (Unavailableexception f) {
  16. Handle exception
  17. }
  18. }
is actually called the servlet's Init method, which is already the servlet code, which is no longer parsed.

The previously mentioned servlet loading is just those servlets that are identified as "load on Start", and when are the other servlets loaded? Check the Initservlet method of Standardwrapper, and view its call hierarchy in eclipse as follows:

You will find a allocate method that calls it indirectly, giving the method call timing diagram After the request has entered wrapper:

As you can see, after the request enters wrapper, a servlet instance is popped from the instance pool stack through the allocate method to handle the request, and the servlet instance is encapsulated as a Filterchain object. It is followed by a series of filter filters to reach the Servlet.service () method, which is the practice of Singlethreadmodel mode. In non-Singlethreadmodel mode, the first time the servlet is loaded and initially initialized to the instance field, the next time the servlet instance is fetched directly from this field, Therefore, the same servlet instance is returned each time in non-singlethreadmodel mode. For a specific introduction to Singlethreadmodel reference: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/SingleThreadModel.html

The wrapper of Tomcat 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.