通过xml配置的方式:
web.xml:
增加如下部分:
<context-param>
<param-name>kkkk</param-name>
<param-value>xxxxxxxx</param-value>
</context-param>
这个属性和值就是context容器中的全局参数了,所以所有的servlet实例都能访问到。
HttpServletDemo02.java:
package com.fl.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HttpServletDemo02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("dddd"); } @Override public void init(ServletConfig config) throws ServletException { ServletContext sc = config.getServletContext(); //增加了这部分。 String temp = sc.getInitParameter("kkkk"); System.out.println(temp); } }