getInitParameter方法
生活随笔
收集整理的這篇文章主要介紹了
getInitParameter方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1. web.xml文件
<?xml?version="1.0"?encoding="UTF-8"?><web-app?id="WebApp_ID"?version="2.5"?xmlns=" http://java.sun.com/xml/ns/javaee "?xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "?xsi:schemaLocation=" http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><context-param><param-name>webParam</param-name><param-value>aaa</param-value></context-param><servlet><servlet-name>test</servlet-name><servlet-class>Test</servlet-class><init-param><param-name>servletParam</param-name><param-value>bbb</param-value></init-param></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/test</url-pattern></servlet-mapping></web-app>?注:區分配置文件中,對參數的配置的不同.?servletParam這個參數是屬于Test這個Servlet的; 而webParam這個參數是屬于web應用的,被各個servlet共享。
? 2. 調用代碼
public?class?Test?extends?HttpServlet?{public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)throws?ServletException,?IOException?{response.setContentType("text/html");PrintWriter?out?=?response.getWriter();String?s1?=?this.getInitParameter("servletParam");String?s2?=?this.getServletContext().getInitParameter("webParam");//注意區別,一個是獨屬于Test這個Servlet的參數,一個是web范圍的參數,out.println("<HTML>");out.println("??<HEAD><TITLE>A?Servlet</TITLE></HEAD>");out.println("??<BODY>");out.println(s1);out.println(s2);out.println("??</BODY>");out.println("</HTML>");out.flush();out.close();} }3. 運行結果
? ? bbb aaa
轉載于:https://my.oschina.net/june6502/blog/224028
總結
以上是生活随笔為你收集整理的getInitParameter方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: numpy中的ogrid
- 下一篇: 如何构建高性能web站点之:分布式缓存