1、在web.xml文件中配置上下文参数
1 2 34 5 8One 6hello 79 Two 10dyf 11
2、在servlet中获取上下文参数
1 //@WebServlet(name="HelloServlet01",value="/Hello") 2 @WebServlet(name="HelloServlet01",urlPatterns="/Hello") 3 public class HelloServlet01 extends HttpServlet { 4 private static final long serialVersionUID = 1L; 5 6 public void doGet(HttpServletRequest request, HttpServletResponse response) 7 throws ServletException, IOException { 8 this.doPost(request, response); 9 10 }11 12 public void doPost(HttpServletRequest request, HttpServletResponse response)13 throws ServletException, IOException {14 //创建一个获取上下文参数的对象15 ServletContext context = this.getServletContext();16 //获取上下文初始化的参数17 String initParameter = context.getInitParameter("One");18 // context.getInitParameterNames();19 PrintWriter writer = response.getWriter();20 //将参数返回到页面21 writer.append(initParameter);22 }23 }