码迷,mamicode.com
首页 > 编程语言 > 详细

[Java.Web][Servlet]读取配置

时间:2017-05-10 00:17:19      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:err   doget   配置   http   let   init   logs   ons   next   

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 = 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]读取配置

标签:err   doget   配置   http   let   init   logs   ons   next   

原文地址:http://www.cnblogs.com/z5337/p/6833177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!