One, pseudo code demo Tomcat's internal code run
1), by mapping to find the content of Servlet-class, string: Com.gqx.servlet.FirstServlet
2), construct Firstservlet object by reflection structure
2, 1 Get byte code (Class) file object
Class clazz=class.forname ("Com.gqx.servlet.FirstServlet");
2, 2 constructs an object by invoking an argument-free construction method
Object obj =clazz.newinstance (); The construction method of the-->serlvet is activated
3), create the ServletConfig object, call the Init method through reflection
3.1 Getting Method objects
Method m = Clazz.getdeclaremethod ("init", servletconfig.class);
3.2 Calling Methods
M.invoke (Obj,config); The Init method of the--2.servlet is called
4) Create a Request,response object to invoke the service method through reflection
4.1 Getting Method objects
METHODM m =clazz.getdeclaremethod ("service", Httpservletrequest.class,httpservletresponse.class);
4.2 Calling methods
M.invoke (Obj,request,response); The service method of the--3.servlet is called
5) Call the Destroy method through reflection when the Tomcat server stops or the Web app is redeployed
5.1 Getting Method objects
Method m = Clazz.getdeclaremethod ("destroy", null);
5.2 Calling methods
M.invoke (Obj,null); --4.servlet's Destroy method is called
using a time series diagram to demonstrate the life cycle of a servlet
Automatic loading of Servlets
By default, the Servlet object is created the first time the servlet is accessed. If more logical code is executed in the servlet's constructor or Init method, the user is slower to access sevrlet the first time. Change the time when the servlet creates an object: Early to load the Web App!!!
in the configuration information of the servlet, add a <load-on-startup>!!
As follows
<servlet> <servlet-name>LifeDemo</servlet-name> <servlet-class> Gz.itcast.c_life. lifedemo</servlet-class> <!--let servlet objects load automatically- <load-on-startup>1</ Load-on-startup> <!--Note: The larger the integer value, the lower the creation priority!! - </servlet>
The Init method with parameters and the Init method without parameters
The Init method, which implements the parameter in the Init method, is called This.init () in the source code, so we usually write the code in the non-parameter Init method.
Multithreading concurrency problems for servlets
Note: The Servlet object is single- instance multithreaded on the Tomcat server.
Because servlets are multithreaded, thread-safety issues can arise when multiple servlet threads concurrently access the servlet's shared data, such as member variables.
Workaround:
1) Synchronize code blocks that are used to share data (using the Synchronized keyword for synchronization)
2) It is recommended that you try not to use member variables in the Servlet class. If you do use members, you must synchronize. and try to narrow the scope of the synchronized code block. (where the member variable is used, where is the synchronization!!) ) to avoid concurrency inefficiencies due to synchronization.
The instance code is as follows:
PackageCom.gqxing.servlet2;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classThreaddemoextendsHttpServlet {/*** Multithreading Security issues * Case Study: Number of visitors to the site*/ intCount=1; Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Write ("You are now the first" +count+ "visitor to the current website! "); //for effect, here's a sleep method to synchronize Threads Try{Thread.Sleep (5000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //a thread-safety issue occurs when multiple threads are accessing the same count value at the same time. count++; }}
Effect
This time to join the mechanism to avoid thread safety
PackageCom.gqxing.servlet2;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classThreadDemo2extendsHttpServlet {/*** Multithreading Security issues * Case Study: Number of visitors to the site*/ intCount=1; Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8"); //loads locks for the currently visited line, synchronized(ThreadDemo2.class) {//The lock thread must be unique and can be used with the current byte-code objectResponse.getwriter (). Write ("You are now the first" +count+ "visitor for the current site!) "); } Count++; }}
Servlet (2)