獲取servlet初始化引數
阿新 • • 發佈:2019-02-05
package cn.itcast.web.servlet;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//通過servletCOnfig獲取servlet配置的初始化引數
//<init-param>
public class ServletDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletConfig config = this.getServletConfig();
//獲取配置的初始化引數的方式1
//String value = config.getInitParameter("charset");
Enumeration e = config.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = config.getInitParameter(name);
System.out.println(name + "=" + value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}