利用ServletContext对象获取web.xml中的全局参数

 1 package com.yxfyg.servlet;
 2 
 3 import java.io.IOException;
 4 import java.util.Enumeration;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 public class Servlet01 extends HttpServlet{
13 
14     @Override
15     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
16         ServletContext context = getServletContext();
17         String address = context.getInitParameter("address");
18         System.out.println("address:" + address);
19         System.out.println("=================");
20         Enumeration<String> initParameterNames = context.getInitParameterNames();
21         while (initParameterNames.hasMoreElements()) {
22             String str = (String) initParameterNames.nextElement();
23             String value = context.getInitParameter(str);
24             System.out.println(str + ":" + value);
25         }
26     }
27     
28     @Override
29     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
30         doGet(req, resp);
31     }
32     
33 }

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12792873.html