首页 > 代码库 > [Java.Web][Servlet]读取配置
[Java.Web][Servlet]读取配置
private ServletConfig config; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String value = config.getInitParameter("data"); OutputStream out = response.getOutputStream(); out.write(value.getBytes()); } @Override public void init(ServletConfig config) throws ServletException { this.config = config; }
String value = http://www.mamicode.com/getServletConfig().getInitParameter("data"); OutputStream out = response.getOutputStream(); out.write(value.getBytes());
OutputStream out = response.getOutputStream(); Enumeration<String> e = this.getServletConfig().getInitParameterNames(); while (e.hasMoreElements()) { // 遍历所有 String name = (String) e.nextElement(); String value = this.getServletConfig().getInitParameter(name); out.write((name + ‘:‘ + value + "\r\n").getBytes()); }
以上代码都可以从 web.xml 中读取配置,配置在如下节点中的 <init-param></init-param> 里:
<servlet> <servlet-name>ServletDemo5</servlet-name> <servlet-class>cn.itcast.ServletDemo5</servlet-class> <init-param> <param-name>data</param-name> <param-value>x1x2x3</param-value> </init-param> </servlet>
[Java.Web][Servlet]读取配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。