1. web.xml文件
webParam aaa test Test servletParam bbb test /test
注:区分配置文件中,对参数的配置的不同. servletParam这个参数是属于Test这个Servlet的; 而webParam这个参数是属于web应用的,被各个servlet共享。
2. 调用代码
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String s1 = this.getInitParameter("servletParam"); String s2 = this.getServletContext().getInitParameter("webParam"); //注意区别,一个是独属于Test这个Servlet的参数,一个是web范围的参数, out.println(""); out.println("A Servlet "); out.println(" "); out.println(s1); out.println(s2); out.println(" "); out.println(""); out.flush(); out.close(); }}
3. 运行结果
bbb aaa