The most important thing to learn about Servlets is to understand the following four points in a servlet:
1. Understanding common interfaces and classes of the Servlet API
2. Mastering the life cycle of the servlet
3. Mastering the deployment and configuration of Servlets
4. User requests (get, post) are processed using the servlet
Using JSP should know that the servlet technology was born before the JSP, Servlet is a Java program, is running on the Java EE server to handle the client and respond to the program.
By looking at the API you can know that the servlet and its sub-interface Genericservlet are seldom used, through the document that it has an abstract class HttpServlet, we must implement it every time we use it, we write a class inherited from this HttpServlet class
The description of its life cycle in the document is: the servlet life cycle:
1. The servlet is constructed and then initialized with the Init method.
2. Any calls from clients to the service method is handled.
3. The servlet is taken out of service, then destroyed with the Destroy method, then garbage collected and finalized.
Execute the constructor first, then execute the init () function, execute the Servic function, and then execute the Doget function:
Packagecom.yc.web.servlets; Importjava.io.IOException; Importjavax.servlet.ServletException; ImportJavax.servlet.http.HttpServlet; Importjavax.servlet.http.HttpServletRequest; ImportJavax.servlet.http.HttpServletResponse; Public classLifeservletsextendshttpservlet{ Publiclifeservlets () {System.out.println ("Construction Method"); }protected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {System.out.println ("Get Method"); } @Overrideprotected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException {System.out.println ("Service Method"); Super. Service (req, resp); } @Override Public voiddestroy () {System.out.println ("Destroy Method"); Super. Destroy (); } @Override Public voidInit ()throwsservletexception {System.out.println ("Init () method"); Super. Init (); } }
Configuration code:
<servlet> <servlet-name>life</servlet-name> <servlet-class> com.yc.web.servlets.lifeservlets</servlet-class> </servlet> <servlet-mapping> <servlet-name>life</servlet-name> <url-pattern>*.do</url-pattern>
By executing this code in a different browser, the servlet is implemented in a singleton pattern, that is, it creates only one instance
This mode has only one instance at a time, and there is no concept of whether or not information is synchronized.
If multiple users access a page of this mode at the same time,
After the visitor has completely executed the page, the visitor does not begin.
So, once the servlet's container is closed (this is Tomcat), the Destroy () method is executed.
First access: Construction, init (), service, Doget ()/dopost ()
Second visit: Service, Doget ()/dopost ()
Destroy: Close the container. Destroy ()
Servlets are single-instance. The thread is not secure.
The servlet Quest