获取Servlet初始化参数
Servlet初始化参数配置
获取Servlet初始化参数2-1
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>org.jbit.servlet.HelloServlet</servlet-class>
<init-param>
<param-name>initParam</param-name>
<param-value>Hello Servlet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
根据参数名称进行读取
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理请求时,doGet()方法被调用。");
String initParam = getInitParameter("initParam");
System.out.println(initParam);
}
获取Servlet上下文参数
配置Servlet上下文
<web-app>
<context-param>
<param-name>contextParam</param-name>
<param-value>Hello Servlet</param-value>
</context-param>
<!--省略其他配置-->
</web-app>
读取Servlet上下文
public class HelloServlet extends HttpServlet {
// …省略其他代码
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理请求时,doGet()方法被调用。");
String initParam = getInitParameter("initParam");
String contextParam =
this.getServletContext().getInitParameter("contextParam");
System.out.println("Servlet初始化参数" + initParam);
System.out.println("系统初始化参数" + contextParam);
}
}