小談-—ServletConfig對象和servletContext對象
阿新 • • 發佈:2017-05-13
就會 獲得 servle log rst 全局 技術 override 對象
一、servletContext概述
servletContext對象是Servlet三大域對象之一,每個Web應用程序都擁有一個ServletContext對象,該對象是Web應用程序的全局對象或者上下文。Tomcat服務器在啟動時,會自動創建一個ServletContext對象,在關閉時,會自動銷毀這個ServletContext對象。每個Web應用程序只擁有一個ServletContext對象,ServletContext對象可以在整個Web應用中共享數據資源。
- 生命周期很長
- 每個web應用都有一個唯一的servletContext對象.
- 在每個應用加載的時候,服務器就會創建servletContext對象。
- ServletContext對象是一個域對象(領域)
- 獲得servletContext的方式
在自定義Servlet中有以下幾種方式獲取到ServletContext對象:
- 通過ServletConfig對象的this.getServletContext()方法獲取。
- 采用request.getSession().getServletContext()方法獲取。
- 通過繼承GenericServlet類或HttpServlet類,調用GenericServlet類或HttpServlet類的getServletContext()方法獲取。
1 import java.io.IOException;2 3 import javax.servlet.ServletConfig; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 //演示獲取servletContext對象 10 /**11 * 有三種方式獲取servletContext對象 12 * 1. 采用servletConfig對象獲取 13 * 2. 采用servlet實例對象獲取 14 * 3. 采用request對象獲取 15 * @author Administrator 16 * 17 */ 18 public class ServletContext1 extends HttpServlet { 19 20 ServletContext sc ; 21 22 @Override 23 public void init(ServletConfig config) throws ServletException { 24 super.init(config) ; 25 sc = config.getServletContext() ;//1. 26 } 27 28 public void doGet(HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException { 30 //第二種方式 31 ServletContext sc1 = this.getServletContext() ; 32 System.out.println(sc); 33 System.out.println(sc1 == sc); 34 //第三種方式 35 ServletContext sc2 = request.getSession().getServletContext() ; 36 System.out.println(sc2 == sc); 37 } 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 doGet(request, response); 42 } 43 }
應用
1) 實現數據共享
2) 獲取全局配置參數
3) 請求轉發
a) 拿取請求轉發器,然後轉發
4) 獲取資源文件
5) 用來獲得文件的MIME的類型.
案例
下面我將通過例子說明:登錄成功後,5秒後跳轉到某個頁面,在頁面中顯示您是第x次登錄成功
- 創建一個VisitServlet用來獲取訪問次數,並存儲在ServletContext對象中。
1 public class VisitServlet extends HttpServlet { 2 @Override 3 public void init() throws ServletException { 4 ServletContext context = getServletContext(); 5 context.setAttribute("times", 0); 6 } 7 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8 ServletContext context = getServletContext(); 9 10 int times = (Integer)context.getAttribute("times"); 11 times ++; 12 context.setAttribute("times", times);
response.setHeader("Refresh", "5;url=/test/showTimeServlet");
13 } 14 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 doGet(request, response); 16 } 17 }
- 創建一個ShowTimeServlet用來顯示訪問次數。
1 public class ShowTimeServlet extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 ServletContext context = getServletContext(); 4 //獲取訪問的次數 5 int times = (Integer)context.getAttribute("times"); 6 7 response.setContentType("text/html;charset=utf-8"); 8 PrintWriter out = response.getWriter(); 9 out.println("<h1>VisitServlet共被訪問了"+times+"次</h1>"); 10 } 11 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 doGet(request, response); 13 } 14 }
獲得Servlet的web.xml配置信息
1 <context-param> 2 <param-name>username</param-name> 3 <param-value>蒼老師</param-value> 4 </context-param> 5 <context-param> 6 <param-name>password</param-name> 7 <param-value>123</param-value> 8 </context-param>
獲取web.xml配置信息中
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 獲取web應用的初始化信息 */ public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲得ServletContext域對象 ServletContext context = this.getServletContext(); //獲得單個數據(username) String name = context.getInitParameter("username") ; // System.out.println(name); //獲得所有數據 Enumeration enumeration = context.getInitParameterNames(); while(enumeration.hasMoreElements()){ String name = (String) enumeration.nextElement(); String value = context.getInitParameter(name); System.out.println(name+":"+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
- 打開瀏覽器,在地址欄輸入http://localhost:8080/test/visit,訪問VisitServlet。
- 再新打開瀏覽器,在地址欄輸入http://localhost:8080/test/show,顯示訪問次數。
二、servletConfig概述
在javax.servlet包中,定義了ServletConfig接口。Servlet容器使用ServletConfig對象在Servlet初始化時向其傳遞配置信息。
所謂的Serlvet配置信息,就是在Web應用程序中web.xml文件中配置有關Servlet的內容。
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>app.java.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/ServletConfigTest</url-pattern> </servlet-mapping> <init-param> <param-name>name</param-name> <param-value>yl</param-value> </init-param> <init-param> <param-name>blog</param-name> <param-value>http://www.longestory.com</param-value> </init-param>
- getServletName()方法:獲取web.xml文件中配置的Servlet名稱。
public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException { ServletConfig servletConfig = this.getServletConfig(); String servletName = servletConfig.getServletName(); System.out.println(servletName); }
運行Web應用程序,在控制臺中打印“ConfigServletTest”。
- getInitParameter(String name)方法:返回指定名稱的初始化參數的值,如果參數不存在則返回null值。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); String name = servletConfig.getInitParameter("name"); String blog = servletConfig.getInitParameter("blog"); System.out.println(name + "‘s blog is " + blog); }
運行Web應用程序,在控制臺中打印“yl‘s blog is http://www.longestory.com”。
- getInitParameterNames()方法:返回Servlet配置的所有初始化參數名稱的枚舉集合. Enumeration是Iterator的前身,用法與Iterator一致。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); Enumeration enumeration = servletConfig.getInitParameterNames(); while (enumeration.hasMoreElements()) { String element = (String) enumeration.nextElement(); String value = servletConfig.getInitParameter(element); System.out.println(element + ": " + value); } }
- ServletConfig有哪些實際作用呢?在struts 1 框架中就運行了ServletConfig內容。
<!-- Standard Action Servlet Configuration (with debugging) --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml, /WEB-INF/struts-config-Wildcard.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2017-05-12
小談-—ServletConfig對象和servletContext對象