Servlet knowledge point (1), servlet knowledge point
Why servlet?
Jsp can be used to develop some websites, but it is only a small website. Because jsp pages are too redundant and hardly well maintained, servlet should be learned to separate display from logic, therefore, we need another technology servlet.
There is web1.0 (there is also a java code in the page to control the output) and web2, 0 (completely separated, with js for control ).
Servlet is a java program that processes client requests and responds to requests on the server.
Sun has developed a set of j2ee standards in the same way as jdbc (database access interface). For server vendors, they must comply with these standards. For development programmers, they must also develop these standards. Therefore, we can cooperate with each other.
Therefore, servlet must comply with this set of standards to run on j2ee.
Servlet Interface
GenericServlet: General Servlet
Httpservlet: supports http
Commonservlet
Servlet is single-instance, that is, multiple accesses only have one Servlet (So avoid writing global variables)
Strus is multi-threaded, and a request for a new action is more efficient.
Servlet lifecycle: initialization-> call-> destruction
Servlet Configuration
(Servlet must be placed in the package, not in the default path)
1. inherit from httpservlet. You must override a method, such as doGet () or doPost ();
2. web. xml configuration:
(1) <servlet>
<Servlet-name> </servlet-name>
<Servlet-class> </servlet-class>
</Servlet>
(2) <servlet-mapping>
<Servlet-mapping>
(Note that the Servlet-name must be consistent during configuration. Servle-class must have a package name ).
A general Servlet lifecycle: (also demonstrated in the form of applets)
First time: first construct-> init ()-> service-> doGet ()/doPost ()
Configure the web. xml file to print the call sequence in the background (the first time) for webpage access)
Second: service-"doGet ()/doPost ()()
Therefore, the Servlet is single-threaded, And the thread is not secure, so global variables are not used.
Destroy: Close the container-"destroy