[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 = 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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步