(1)lode-on-startup,该Servlet会在项目启动时被调用(主要调用起init方法,为安全着想,一般不应该为该Servlet建立URL映射),一般用作预处理一些数据,或者配合多线程建立定时任务
<servlet><pre name="code" class="html"><span style="font-family:Arial, Helvetica, sans-serif;"> </span><span style="font-family:SimSun;font-size:12px;"> <servlet-name>FirstServlet</servlet-name></span>
<servlet-class>com.hunhun.FirstServlet</servlet-class> <!-- 1 表示多个预处理Servlet的启动顺序--> <lode-on-startup>1</lode-on-startup> </servlet>
(2)init-param,可用做Servlet的一些配置信息
<servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.hunhun.FirstServlet</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </servlet>
可以在FirstServlet的do*方法中使用this.getServletConfig().getInitParameter("encoding");获取web.xml中encoding值utf-8
2.1 多个init-param
<servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.hunhun.FirstServlet</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>author</param-name> <param-value>hunhun</param-value> </init-param> <init-param> <param-name>versioon</param-name> <param-value>1.0</param-value> </init-param> </servlet>
可以在FirstServlet的do*方法中
Enumeration<String> names = this.getServletConfig().getInitParameterNames(); while(names.hasMoreElements()){ //name为每个init-param名字 String name=names.nextElement(); }
(3)context-param,全局配置信息,可在多个Servlet中使用
<servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.hunhun.FirstServlet</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>author</param-name> <param-value>hunhun</param-value> </init-param> <init-param> <param-name>versioon</param-name> <param-value>1.0</param-value> </init-param> </servlet> <context-param> <param-name>date</param-name> <param-value>2011-12-3 10:30:24</param-value> </context-param>
可以在*Servlet的do*方法中使用String date = this.getServletContext().getInitParameter("date");获取到日期
时间: 2024-10-25 21:12:15