Servlet Servlet interface of the specification, define the main scope of the method.
1, public void init (ServletConfig servletConfig) (initialization)
Effect parameters:
(1) call the object's methods ServletConfig, get the name of the Servlet:
Profiles:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>MyServletname</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServletname</servlet-name> <url-pattern>/abc</url-pattern> </servlet-mapping> </web-app>
public void init(ServletConfig servletConfig) throws ServletException { String servletName=servletConfig.getServletName(); System.out.println(servletName); }
operation result:
(2) obtaining initialization parameter configuration file:
<servlet> <servlet-name>MyServletname</servlet-name> <servlet-class>MyServlet</servlet-class> <init-param> <param-name>zhai</param-name> <param-value>I love study</param-value> </init-param> </servlet>
public void init(ServletConfig servletConfig) throws ServletException { String paramvalue =servletConfig.getInitParameter("zhai"); System.out.println(paramvalue); }
(3) This method is only executed when the servlet object is created (refresh the browser)
2、public void service(ServletRequest servletRequest, ServletResponse servletResponse)方法:
ServletRequest: representative requested information.
ServletResponse: represents the response information.
Every time an object is created servletRequest and servletResponse.
3、public void destroy()方法:
When executed destroyed servlet objects.
4, servlet life cycle:
Initialization function only be executed if the first visit, service function will be executed each time it is accessed.
dastory function when the object is destroyed (closed server).