Chapter 5 servlet container chapter 5th discusses the container module. Container refers to the org. Apache. Catalina. Container interface, which has four types: Engine, host, context, and wrapper. This chapter provides two programs that work on context and wrapper.
Containers are divided into four categories. The class diagram is as follows:
A wrapper is a servlet;
A context contains several wrapper;
This chapter consists of two parts: wrapper and context.
The first part of the wrapper chapter is the detailed decomposition of the invoke method in Chapter 4 simplecontainer!
public class SimpleWrapper implements Wrapper, Pipeline { // the servlet instance private Servlet instance = null; private String servletClass; private Loader loader; private String name; private SimplePipeline pipeline = new SimplePipeline(this); protected Container parent = null; public SimpleWrapper() { pipeline.setBasic(new SimpleWrapperValve()); } public synchronized void addValve(Valve valve) { pipeline.addValve(valve); } ...}
Now that we talk about the container, we have to talk about the pipeline (each level of container has a pipeline); compare our commands to the pipeline, before the (flow) command comes into contact with the final servlet, there will be a long pipe (simplepipeline), pipeline has a valve (valve), each valve will do a task! In this case, there is a basic valve (simplewrappervalve) in the pipeline, and this basic valve is used to generate Servlet and call its service method.
The wrapper program class diagram is as follows:
The procedure is as follows:
Call the invoke of wrapper first;
SimpleWrapper.java public void invoke(Request request, Response response) throws IOException, ServletException { pipeline.invoke(request, response); }
Call the invoke of the MPs queue;
Simplepipeline. java public void invoke (request, response) throws ioexception, servletexception {// invoke the first valve in this pipeline for this request (New simplepipelinevalvecontext ()). invokenext (request, response);} simplepipelinevalvecontext is the internal class of simplepipeline. It is used to cycle all valves, and finally call the basic valve (basic in the code below) simplepipelinevalvecontext. java public void invokenext (request, respons E response) throws ioexception, servletexception {int subscript = stage; Stage = stage + 1; // invoke the requested valve for the current request thread if (subscript <valves. length) {valves [subscript]. invoke (request, response, this);} else if (subscript = valves. length) & (Basic! = NULL) {basic. Invoke (request, response, this);} else {Throw new servletexception ("no valve ");}}
This part of the basic valve is simplewrappervalve (specified when constructing simplewrapper), the basic valve will call reflection to generate servlet class ......
The second part of the context class diagram is as follows:
Most web programs cannot have only one servlet. Multiple servlets constitute a context.
In other words, a context contains multiple wrappers.
Now there is a problem. Multiple wrappers have to have a record. Request 1 should be handled by the wrapper, and request 2 should be processed by the wrapper.
So we have the ER interface. Here we use its implementation class, simplecontextmapper. The map method returns the corresponding wrapper.
public Container map(Request request, boolean update) { String requestURI = ((HttpRequest) request).getDecodedRequestURI(); String relativeURI = requestURI.substring(contextPath.length()); Wrapper wrapper = null; String name = context.findServletMapping(relativeURI); if (name != null) wrapper = (Wrapper) context.findChild(name); return (wrapper); }
Finding wrapper is the same as the previous process.
Chapter 6 lifecycle Chapter 1 describes the lifecycle interface. This interface defines the lifecycle of a Catalina component and provides an elegant way to notify other components of events that occur on the component. In addition, the lifecycle interface provides an elegant mechanism for starting and stopping components in Catalina through a single start/stop.
Class Diagram in this chapter:
Tomcat is a componentized software. All the components have implemented the lifecycle interface, which contains the start and stop methods. The effect we want now is that I can start all the components by starting only one component system, the same is true for disabling. It looks complicated. It's actually very simple.
public synchronized void start() throws LifecycleException { if (started) throw new LifecycleException("SimpleContext has already started"); // Notify our interested LifecycleListeners lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); started = true; try { // Start our subordinate components, if any if ((loader != null) && (loader instanceof Lifecycle)) ((Lifecycle) loader).start(); // Start our child containers, if any Container children[] = findChildren(); for (int i = 0; i < children.length; i++) { if (children[i] instanceof Lifecycle) ((Lifecycle) children[i]).start(); } // Start the Valves in our pipeline (including the basic), // if any if (pipeline instanceof Lifecycle) ((Lifecycle) pipeline).start(); // Notify our interested LifecycleListeners lifecycle.fireLifecycleEvent(START_EVENT, null); } catch (Exception e) { e.printStackTrace(); }
The components in simplecontext are started in turn;
In fact, if this chapter is just about the life cycle, it will end here, but if it is about the observer mode, it will be much more.
The so-called observer pattern, simply put, means that when I have an action, I need to notify people who care about me. That's simple.
Bootstrap.java LifecycleListener listener = new SimpleContextLifecycleListener(); ((Lifecycle) context).addLifecycleListener(listener);simplecontext.javaprotected LifecycleSupport lifecycle = new LifecycleSupport(this);public void addLifecycleListener(LifecycleListener listener) { lifecycle.addLifecycleListener(listener); }
During running, the following code tells all the listeners who care about simplecontext: The simplecontext class has done the before_start_event action!
Lifecycle. firelifecycleevent (before_start_event, null); lifecyclesupport. java public void firelifecycleevent (string type, object data) {lifecycleevent event = new lifecycleevent (lifecycle, type, data); lifecyclelistener interested [] = NULL; synchronized (listeners) {interested = (lifecyclelistener []) listeners. clone () ;}for (INT I = 0; I <interested. length; I ++) // cyclically notifies all consumers of interested [I]. lifecycleevent (event);} // a specific publisher public class implements attributes {@ suppresswarnings ("UNUSED") Public void lifecycleevent (lifecycleevent event) {lifecycle = event. getlifecycle (); system. out. println ("simplecontextlifecyclelistener's event" + event. getType (). tostring (); If (lifecycle. start_event.equals (event. getType () {system. out. println ("Starting context. ");} else if (lifecycle. stop_event.equals (event. getType () {system. out. println ("Stopping context. ");}}}
How Tomcat works summary 2