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);
	}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM