Java如何读取servlet上下文初始化参数?
package org.nhooo.example.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.Servlet; 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 ContextInitParameter extends HttpServlet implements Servlet { public ContextInitParameter() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); //获取ServletContext的实例 ServletContext context = getServletContext(); //要读取上下文初始化参数,我们可以调用context.getInitParameter() //并传递要读取的初始化参数的名称。如果命名 //参数不存在,返回值将为null。 // //在此示例中,我们读取了一个称为LOG.PATH的初始化参数。 String logPath = context.getInitParameter("LOG.PATH"); writer.println("Log Path: " + logPath + "<br/>"); //读取Servlet初始化参数的所有名称。如果 //servlet没有任何空的枚举将被返回。 Enumeration enumeration = context.getInitParameterNames(); while (enumeration.hasMoreElements()) { String paramName = (String) enumeration.nextElement(); String paramValue = context.getInitParameter(paramName); writer.println("Context Init Param: [" + paramName + " = " + paramValue + "]<br/>"); } } }