ServletContext 对象
ServletContext 對象
問題: Request 解決了一次請求內的數據共享問題,session 解決了 用戶不同請求的數據共享問題,那么不同的用戶的數據? ? ? ? ? ? ? 共享該怎么辦呢?
解決: 使用 ServletContext 對象
作用: 解決了不同用戶的數據共享問題
原理:ServletContext 對象由服務器進行創建,一個項目只有一個對象。不管在項目的任意位置進行獲取得到的都是同一個對象,那 么不同用戶發起的請求獲取到的也就是同一個對象了,該對象由 用戶共同擁有。
特點: 服務器進行創建
? ? ? ? ? ??用戶共享
? ? ? ? ? ??一個項目只有一個
生命周期: 服務器啟動到服務器關閉
作用域:項目內
使用:獲取 ServletContext 對象
? ? ? ? ? 使用作用域進行共享數據流轉
? ? ? ? ? 獲取 web.xml 中的全局配置
? ? ? ? ? 獲取 webroot 下項目資源流對象
? ? ? ? ? 獲取 webroot 下資源絕對路徑
案例:網頁瀏覽器次數統計
? ? ? //第一種方式(常用)
?? ??? ??? ?ServletContext sc=this.getServletContext();
?? ???//第二種方式
?? ??? ??? ?ServletContext sc2=this.getServletConfig().getServletContext();
?? ??? //第三種方式(常用)
?? ??? ??? ?ServletContext sc3=req.getSession().getServletContext();?
* ServletContext對象學習:
?* ?? ??? ?問題:
?* ?? ??? ??? ?不同的用戶使用相同的數據
?* ?? ??? ?解決:
?* ?? ??? ??? ?ServletContext對象
?* ?? ??? ?特點:
?* ?? ??? ??? ?服務器創建
?* ?? ??? ??? ?用戶共享
?* ?? ??? ?作用域:
?* ?? ??? ??? ?整個項目內
?* ?? ??? ?生命周期:
?* ?? ??? ??? ?服務器啟動到服務器關閉
?* ?? ??? ?使用:
?* ?? ??? ??? ?獲取ServletContext對象
?* ?? ??? ??? ??? ??? ?//第一種方式:
?? ??? ??? ??? ??? ??? ?ServletContext sc=this.getServletContext();
?? ??? ??? ??? ??? ?//第二種方式:
?? ??? ??? ??? ??? ??? ?ServletContext sc2=this.getServletConfig().getServletContext();
?? ??? ??? ??? ??? ?//第三種方式:
?? ??? ??? ??? ??? ??? ?ServletContext sc3=req.getSession().getServletContext();
?* ?? ??? ??? ?使用ServletContext對象完成數據共享
?* ?? ??? ??? ??? ??? ?//數據存儲
?* ?? ??? ??? ??? ??? ??? ?sc.setAttribute(String name, Object value);
?* ?? ??? ??? ??? ??? ?//數據獲取
?* ?? ??? ??? ??? ??? ??? ?sc.getAttribute("str") 返回的是Object類型
?* ?? ??? ??? ??? ??? ?注意:
?* ?? ??? ??? ??? ??? ??? ?不同的用戶可以給ServletContext對象進行數據的存取。
?* ?? ??? ??? ??? ??? ??? ?獲取的數據不存在返回null。
?* ?? ??? ??? ?獲取項目中web.xml文件中的全局配置數據
?* ?? ??? ??? ??? ??? ?sc.getInitParameter(String name); 根據鍵的名字返回web.xml中配置的全局數據的值,返回String類型。
?* ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ? ?如果數據不存在返回null。
?* ?? ??? ??? ??? ??? ?sc.getInitParameterNames();返回鍵名的枚舉
?* ?? ??? ??? ??? ?配置方式:注意 一組<context-param>標簽只能存儲一組鍵值對數據,多組可以聲明多個 ?<context-param>進行存儲。
?* ?? ??? ??? ??? ??? ? ?<context-param>
?? ??? ??? ??? ??? ??? ? ??? ?<param-name>name</param-name>
?? ??? ??? ??? ??? ??? ? ??? ?<param-value>zhangsan</param-value>
? ?? ??? ??? ??? ??? ? ?</context-param>
? ?? ??? ??? ??? ?作用:將靜態數據和代碼進行解耦。
? ?? ??? ??? ?獲取項目webroot下的資源的絕對路徑。
? ?? ??? ??? ??? ?String path=sc.getRealPath(String path);?? ?
? ?? ??? ??? ??? ?獲取的路徑為項目根目錄,path參數為項目根目錄中的路徑
? ?? ??? ??? ?獲取webroot下的資源的流對象
? ?? ??? ??? ??? ?InputStream is = sc.getResourceAsStream(String path);
? ?? ??? ??? ??? ?注意:
? ?? ??? ??? ??? ??? ?此種方式只能獲取項目根目錄下的資源流對象,class文件的流對象需要使用類加載器獲取。
? ?? ??? ??? ??? ??? ?path參數為項目根目錄中的路徑
?
ServletContextServlet.java?
? package com.dym.servlet;import java.io.IOException; import java.io.InputStream;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class ServletContextServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//獲取servletContext對象//第一種方式(常用)ServletContext sc=this.getServletContext();//第二種方式ServletContext sc2=this.getServletConfig().getServletContext();//第三種方式(常用)ServletContext sc3=req.getSession().getServletContext();System.out.println(sc==sc2);System.out.println(sc==sc3);//使用ServletContext對象完成數據共享//數據存儲sc.setAttribute("str", "ServletContext對象學習");//獲取項目web.xml的全局配置數據String str=sc.getInitParameter("name");System.out.println("全局配置參數"+str);//獲取項目根目錄下的資源的絕對路徑String path=sc.getRealPath("/doc/1.txt");System.out.println(path);//獲取項目根目錄下資源的流對象InputStream is=sc.getResourceAsStream("/doc/1.txt");} }?web.xml?
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><!--全局配置數據 --><context-param><param-name>name</param-name><param-value>zhixi</param-value></context-param><display-name>06-ServletContent</display-name><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>ServletContextServlet</servlet-name><servlet-class>com.dym.servlet.ServletContextServlet</servlet-class></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>ServletContextServlet2</servlet-name><servlet-class>com.dym.servlet.ServletContextServlet2</servlet-class></servlet><servlet-mapping><servlet-name>ServletContextServlet</servlet-name><url-pattern>/context</url-pattern></servlet-mapping><servlet-mapping><servlet-name>ServletContextServlet2</servlet-name><url-pattern>/context2</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>ServletContextServlet2.java
package com.dym.servlet;import java.io.IOException;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class ServletContextServlet2 extends HttpServlet {@Overrideprotected void service(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {//創建ServletContext對象ServletContext sc=this.getServletContext();//獲取數據System.out.println("ServletContextServlet2.service()"+sc.getAttribute("str"));} } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的ServletContext 对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: myeclipse怎么如何激活
- 下一篇: ServletConfig 对象