Servlet
1. The essence of Servlet
Servlet is essentially a java interface. By implementing the servlet interface, the custom servlet class can accept the data submitted by the client, respond to the client's request and output information to the client.
2. Function
-
Read explicit data sent by the client (browser). This includes HTML forms on Web pages, or forms from applet s or custom HTTP clients
-
Read the implicit HTTP request data sent by the client (browser). This includes cookies, media types, compressed formats that browsers can understand, and so on
-
Send explicit data (i.e. documents) to the client (browser). The format of the document can be various, including text file (HTML or XML), binary file (GIF image), Excel, etc
-
Send an implicit HTTP response to the client (browser). This includes telling browsers or other clients the type of document returned (such as HTML), setting cookies and cache parameters, and other similar tasks.
3. Structure inheritance diagram
4.Servlet life cycle
The Servlet life cycle can be defined as the whole process from creation to destruction
-
Execute Servlet constructor method
-
Execute init() initialization method
(steps 1 and 2 are called when the Servlet is created during the first access)
-
Execute the service() method (step 3, which will be called for each access)
-
Execute the destroy() destroy method (called when the web project stops)
5.Servlet instance
In actual development, we usually write our own Servlet program by inheriting HttpServlet class
-
Write a class to inherit the HttpServlet class
-
Override the doGet() or doPost() methods according to business needs
-
Configure the access address of the Servlet program in web/WEB-INF/web.xml
//1. Write a class to inherit HttpServlet public class MyServlet extends HttpServlet { //2. Override doGet() or doPost() @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("get request"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("post request"); } }
<servlet> <!--Custom Servlet Class name of the program --> <servlet-name>MyServlet</servlet-name> <!--Custom Servlet Full class name of the program: package name.Class name --> <servlet-class>TestServlet.MyServlet</servlet-class> </servlet> <!-- Matching pattern--> <servlet-mapping> <!-- custom servlet Name of--> <servlet-name>MyServlet</servlet-name> <!-- When url by http://The servlet program is triggered when localhost:8080/hello -- > <url-pattern>/hello</url-pattern> </servlet-mapping>
6.ServletConfig
ServletConfig: represents the configuration information of the current Servlet in web.xml
-
Get the name of the current Servlet in web.xml: servletConfig.getServletName() return value String
-
Get initialization parameters: servletconfig.getinitparameter (String name) return value String
-
Get the ServletContext object representing the current web application: ServletConfig.egtServletContext() return value ServletContext
<servlet> <!-- custom servlet Class name of--> <servlet-name>MyServlet</servlet-name> <!-- custom servlet Full class name for--> <servlet-class>TestServlet.MyServlet</servlet-class> <!-- servlet Initialization parameters for servlet Can pass servletConfig.getInitParamter(key)obtain--> <init-param> <param-name>username</param-name> <param-value>root</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>root</param-value> </init-param> </servlet>
public void init(ServletConfig servletConfig) throws ServletException { //Function 1: get the alias of Servlet: System.out.println(servletConfig.getServletName()); //Function 2: get initialization parameters System.out.println("user name:"+servletConfig.getInitParameter("username")); System.out.println("password:"+servletConfig.getInitParameter("password")); //Function 3: get context object System.out.println(servletConfig.getServletContext()); }
7.ServletContext
7.1 what is ServletContext
-
ServletContext is an interface that represents a Servlet context object
-
A web project has only one ServletContext object instance
-
The ServletContext object is a domain object
Domain object: an object that can access data like a Map. It is called a domain object. The domain here refers to the operation range of accessing data
7.2 function of ServletContext class
-
Get the context parameter context param configured in web.xml: getinitparameter (key)
-
Get the current path of the project, format: / Project path
-
Get the absolute path on the server hard disk after project deployment
-
Access data like map
<context-param> <param-name>username</param-name> <param-value>Zhang San</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>123</param-value> </context-param>
public class MyServlet extends HttpServlet { //Override doGet() protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //ServletContext has three functions: //1. Get the parameter context param configured in web.xml ServletConfig servletConfig = getServletConfig(); System.out.println("user name:"+servletConfig.getServletContext().getInitParameter("username")); System.out.println("password:"+servletConfig.getServletContext().getInitParameter("password")); //2. Obtain the current project path: System.out.println(servletConfig.getServletContext().getContextPath()); //3. Obtain the absolute address on the hard disk after project deployment: "/" idea is mapped in the project web directory System.out.println(servletConfig.getServletContext().getRealPath("/")); }
Note: there is only one data in a web project in ServletContext. It is started when the project is deployed, destroyed when the project is stopped, and the data will be lost when the server is restarted.