Develop a servlet:
1.1 Steps:
1) Write Java class, Inherit HttpServlet class
2) re-doget and Dopost methods
3) servlet program to the Tomcat server to run!!
Copy the 3.1 servlet program's class code to the Web-inf/classes directory
3.2 Configuring in the Web. xml file
<!--Configure the configuration of a servlet--<!--servlet-- <servlet> <!--The internal name of the servlet, custom. Try to make sense -<servlet-name>FirstServlet</servlet-name> <!--servlet class full Name: Package name + Simple class name-- <servlet-class>servlet. firstservlet</servlet-class> </servlet> <!--servlet mapping configuration-- < Servlet-mapping> <!--The internal name of the servlet, be sure to align with the internal name above!! - <servlet-name>FirstServlet</servlet-name> <!--servlet mapping path (name of Access servlet)-- <url-pattern>/first</url-pattern> </servlet-mapping>
/first the resource name.
1) Find out if there is a matching url-pattern content (/first) in the DAY10 Web. xml
2) If a matching url-pattern is found, use the name of the current Servlet-name to query the Web. xml file for the same name.
Servlet configuration
3) If found, remove the Servlet-class content from the corresponding servlet configuration information: string:servlet. Firstservlet
by Reflection:
A) construct the Firstservlet object
b) and then call Firstservlet inside the method
servlet Mapping Path
<servlet-mapping> <!--The internal name of the servlet, be sure to align with the internal name above!! - <servlet-name>FirstServlet</servlet-name> <!--servlet mapping path (name of Access servlet)-- <url-pattern>/first</url-pattern> </servlet-mapping>
servlet Default Path
<url-pattern> is a default path within the servlet that corresponds to a defualtservlet. Used to parse a static file. If the last file name is entered incorrectly and there is no matching url-pattern, the tomcat default path is executed, and default auto-search returns 404 page if not found
The Servlet life cycle is controlled by the Tomcat server
Construction method: Create Servet first time access is created once
Init: Called only once after the Servlet method has been created
Service: called multiple times. Call N times
Destroy: Called when the Servlet object is destroyed. Destroy the Servlet object when you stop the server or redeploy the Web App. Called only 1 times.
Servlet Life cycle Demo: Tomcat Internal call process ( understanding ):
1) by mapping to find the Servlet-class content, just the servlet in the code . Firstservlet
2) Constructing Firstservlet objects by reflection
2.1 Get the byte code object class Clazz=class.forname ("Servlet.firstservlet");
2.2 To construct an object by calling the parameterless construction method
Object obj = clazz.newinstance (); The constructor method of the--1.servlet is called
3) Create a ServletConfig object to invoke 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
Method M=clazz.getdeclaremethod ("service", Httpservletrequest.class,httpservletresponse.class);
4.2 Calling methods
M.invoke (obj.request,response); --3.servlet Service method invocation
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
servlet Auto Load
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>!!
How servlet life cycle Works