servlet 初始化参数和上下文参数
1 <!-- init(初始化)参数的调用 --> 2 <servlet> 3 <servlet-name>InitTest</servlet-name> 4 <servlet-class>com.cqq.servlet.InitTestServlet</servlet-class> 5 <init-param> 6 <param-name>getInit</param-name> 7 <param-value>Init的value</param-value> 8 </init-param> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>InitTest</servlet-name> 12 <url-pattern>/InitTest</url-pattern> 13 </servlet-mapping>
<!-- 上下文参数 --> <context-param> <param-name>contextPrm</param-name> <param-value>这是上下文参数context param</param-value> </context-param>
调用的方法示例
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String getInit=getInitParameter("getInit");//初始化参数 resp.setContentType("text");//不加乱码 resp.getWriter().print(getInit); String contextprm=this.getServletContext().getInitParameter("contextPrm");//上下文参数 resp.getWriter().println(contextprm); } }