Brief introduction to the basic concept of Servlet

Keywords: Java xml JavaEE Tomcat

Basic concepts of Servlet

The Concept of Servlet

http protocol acts on client-server. Request is sent by the client, and Response is sent to the client after the server receives the data. This is the request-Response mode. But the request Response must be handled by a mediation, which is a server-side applet called servlet.

 

Development and Use of Servlet

To create a servlet, an abstract class HttpServlet must be used. The abstract class HttpServlet has no abstract method. It is a class of template design pattern, and the template method is service(). Using this class, you must import the package provided by Tomcat server (I used Tomcat 8 imported package is mysql-connector-java-5.1.25), the specific import method

When imported, you can write the servlet properly.

However, this is only the code that can write servlet normally. Of course, when the code works, it needs data exchange of request and response. Therefore, it is necessary to set up a processing path. The processing path is configurated in the web.xml file, which is in the "webContent/WEB-INF" directory, and the specific configuration is as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>MvcPro</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <!-- Configure the corresponding path in the container servlet -->
13   <servlet>
14   <!-- Configuration name-->
15   <servlet-name>empServlet</servlet-name>
16   <!-- Configure the specified path,Corresponding servlet Where is the file for the implementation class -->
17   <servlet-class>com.testmvc.pro.empServlet.EmpServlet</servlet-class>
18   </servlet>
19   <!-- Definition empServlet The mapping path of -->
20   <servlet-mapping>
21   <!-- The configuration name of the path to be defined must be consistent -->
22   <servlet-name>empServlet</servlet-name>
23   <!-- The name of the mapping path is encoded using emp replace com.testmvc.pro.empServlet.EmpServlet-->
24   <url-pattern>/emp/*</url-pattern>
25   </servlet-mapping>
26 </web-app>

 

Servlet life cycle

There are five stages in the servlet's life cycle: loading - > instantiation - > initialization - > request processing - > destruction. The loading stage is invisible, init method is invoked in initialization, service method is invoked in service stage, destroy method is invoked in destruction stage.

Running mechanism of Servlet

Because servlet s inherit HttpServlet classes without abstract methods (there are no abstract methods, but there are many other methods), they can call corresponding methods (doget, dopost, dodelete, etc.) by calling the corresponding service() template method to determine the client's request. Of course, we can also override the methods of the parent class ourselves. It must be noted here that after overwriting the methods of the parent class, the methods in the parent template will not be invoked. So don't rewrite it after you don't have special needs. Of course, if you want to add a little decision that can be overridden, you call the same name method of the parent class through super.

Posted by misschristina95 on Sat, 04 May 2019 00:20:37 -0700