How to obtain configuration information for servlet objects

Definition and role ServletConfig:
ServletConfig: the object represents the servlet configuration information, a Servlet only a ServletConfig object

//1.获取ServletConfig对象
ServletConfig config = this.getServletConfig();


// 2 using
System.out.println (config.getServletName ()); // get the name of the servlet

System.out.println (config.getInitParameter ( "name")); // Get request or initialization parameters (value acquired by the corresponding key)

Enumeration <String> initParameterNames = config.getInitParameterNames (); // for acquiring ServletContext object

Set of initialization parameters following two methods:

① directly disposed in the servlet
@WebServlet (initParams = {@ WebInitParam ( name = "name", value = "zhangsan")}, value = "/ tscf")

② set in the web.xml file

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>cn.luo.servlet.TestServletConfig</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>zhangsan</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/tscf</url-pattern>
</servlet-mapping>

Guess you like

Origin www.cnblogs.com/su-chu-zhi-151/p/11199783.html
Recommended