1、獲取web 程序啟動時初始化參數
2、實現多個Servlet 對象共享數據
測試:
3、讀取web應用下的資源
4、請求轉發
總結
當Servlet 容器啟動的時候 會為每個web應用創建一個ServletContext 對象代表當前的web應用。
在web.xml 文件中不止可以配置Servlet的初始化信息 還可以給整個web應用配置初始化信息。
web.xml 設置需要初始化的參數
<!--1、獲取web應用程序初始化參數--><context-param><param-name>name</param-name><param-value>crush</param-value></context-param><context-param><param-name>school</param-name><param-value>hngy</param-value></context-param>
寫一個Servlet繼承HttpServlet
/***@Author:crush*@Date:2021-05-0916:32*version1.0*/@WebServlet("/servlet")publicclassServletContextTestextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//設置響應頭resp.setContentType("text/html;charset=utf-8");PrintWriterwriter=resp.getWriter();ServletContextservletContext=this.getServletContext();//根據名稱獲取單個初始化參數Stringparameter=servletContext.getInitParameter("name");writer.print(parameter);writer.print("<br><hr>");//獲取全部初始化參數Enumeration<String>initParameterNames=servletContext.getInitParameterNames();while(initParameterNames.hasMoreElements()){Stringname=initParameterNames.nextElement();Stringvalue=servletContext.getInitParameter(name);writer.print(name+":"+value);writer.print("<hr>");}}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
@WebServlet("/servlet6") 作用等于
<servlet><servlet-name>/servlet1</servlet-name><servlet-class>com.crush.servlet.ServletContextTest</servlet-class></servlet><servlet-mapping><servlet-name>/servlet1</servlet-name><url-pattern>/servlet1</url-pattern></servlet-mapping>
一個web 應用中所有Servlet都共享ServletContext對象。在一定時候,ServletContext 也可以拿來傳遞信息
或者全局都需要的對象或者數據可以放進ServletContext中。
ServletContext接口的方法:這里講解增加、獲取、刪除、設置ServletContext 域屬性四個方法。
方法 | 描述 |
---|---|
Enumeration getAttributeNames(); | 返回一個Enumeration其中包含該ServletContext中所有的屬性名稱 |
Object getAttribute(String name); | 返回具有給定名稱的servlet容器屬性; |
void removeAttribute(String name); | 從此ServletContext中刪除具有給定名稱的屬性。 |
setAttribute(String name,Object obj) | 在此ServletContext中將對象綁定到給定的屬性名稱。 如果指定的名稱已經用于屬性,則此方法將使用新的屬性替換該屬性。 如果在ServletContext上配置了偵聽器,則容器會相應地通知它們。 |
設置值: ServletContextTest1
/***@Author:crush*@Date:2021-05-0916:59*version1.0*/@WebServlet("/servlet1")publicclassServletContextTest1extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//獲取ServletContext對象ServletContextservletContext=this.getServletContext();//設置值ServletContext域屬性name域屬性名obj是值//往ServletContext中放進username=crush這個鍵值對servletContext.setAttribute("username","crush");//在控制臺給出提示System.out.println("值已經設置完成");//重定向//resp.sendRedirect("/servlet2");//轉發req.getRequestDispatcher("servlet2").forward(req,resp);}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
取出值 :ServletContextTest2
/***@Author:crush*@Date:2021-05-0916:59*version1.0*/@WebServlet("/servlet2")publicclassServletContextTest2extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//獲取ServletContext對象ServletContextservletContext=this.getServletContext();//通過之前的設置的名字取出username的值Stringusername=(String)servletContext.getAttribute("username");PrintWriterwriter=resp.getWriter();writer.print(username);//返回一個Enumeration其中包含該ServletContext中可用的屬性名稱//Enumeration<String>attributeNames=servletContext.getAttributeNames();//從此ServletContext中刪除具有給定名稱的屬性。//servletContext.removeAttribute("");}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
先訪問 /servlet1 存值進去
再訪問 /servlet2 取值出來
使用ServletContext 可以讀取web應用下的資源
常用方法
方法 | 描述 |
---|---|
String getRealPath(String path); | 獲取與給定虛擬路徑相對應的真實路徑。 |
InputStream getResourceAsStream(String path); | 返回位于指定路徑處的資源作為InputStream對象。 |
URL getResource(String path) | 返回映射到給定路徑的資源的URL。 |
MySQL.properties
user=rootpassword=123456url=jdbc:mysql://localhost:3306/mysqldrive=com
讀取資源文件
/***讀取資源內容*@Author:crush*@Date:2021-05-0916:59*version1.0*/@WebServlet("/servlet3")publicclassServletContextTest3extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{resp.setContentType("text/html;charset=utf-8");ServletContextservletContext=this.getServletContext();//獲取相對路徑中的輸入流對象是要獲取web應用程序的路徑InputStreaminputStream=servletContext.getResourceAsStream("\\WEB-INF\\classes\\mysql.properties");//資源類對象Propertiesproperties=newProperties();//load從輸入字節流中讀取屬性列表(鍵和元素對)properties.load(inputStream);PrintWriterwriter=resp.getWriter();writer.print("user"+properties.getProperty("user")+"<br>");writer.print("password"+properties.getProperty("password")+"<br>");writer.print("url"+properties.getProperty("url")+"<br>");writer.print("drive"+properties.getProperty("drive")+"<br>");}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
對于mysql. properties 為什么是從WEB-INF/classes/ 目錄下取的解釋
resources和java文件夾下的 在web程序下的路徑都是classes的路徑下的 到之后我們會學到classpath的 一個路徑
會更了解一些。
第二種方式
直接將mysql.properties文件放在WEB-INF/目錄下
這個時候取的路徑就產生了變化了,可以直接那么取到
這個時候我們發現 如果文件是放在WEB-INF 下面 的話 編譯完后 是直接就在WEB-INF 下面 而不是在classes目錄下的。這個內容是maven 里的內容,好奇可以去查一查。
ServletContextTest5 轉發到 ServletContextTest 去
/***請求轉發*@Author:crush*@Date:2021-05-0916:59*version1.0*/@WebServlet("/servlet5")publicclassServletContextTest5extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{resp.setContentType("text/html;charset=utf-8");ServletContextservletContext=this.getServletContext();//RequestDispatcher對象可用于將請求轉發到資源或將資源包括在響應中。資源可以是動態的也可以是靜態的。//路徑名必須以/開頭,并被解釋為相對于當前上下文根。使用getContext獲取外部上下文中資源的RequestDispatcherservletContext.getRequestDispatcher("/servlet").forward(req,resp);//forward:將請求從servlet轉發到服務器上的另一個資源(servlet,JSP文件或HTML文件)}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
亂碼解決
/***亂碼問題*@Author:crush*@Date:2021-05-0916:59*version1.0*/@WebServlet("/servlet6")publicclassServletContextTest6extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//解決亂碼只是解決單獨的響應亂碼如果有請求還要設置請求的編碼req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");PrintWriterwriter=resp.getWriter();writer.print("你好啊,JavaWeb");}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doGet(req,resp);}}
到此,相信大家對“JavaWeb中ServletContext的介紹和應用”有了更深的了解,不妨來實際操作一番吧!這里是本站網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
國企高管的薪酬規定有哪些?新規定的內容涉及國有企業負責人的薪酬結構、薪酬體系、基本年薪、績效年薪、補充保險、崗位消費標準模式和監督體系。重點是奠定我國國有企業高管薪酬體系的總體框架和薪酬水平的大致范圍。鑒于金融業的特殊性,本規定頒布后,相關主管部門將針對金融和非金融企業等發布具體實施細則。各地也將參照本規定出臺相關地方性法規,全面規范國有企業負責人的薪酬管理。與員工平均工資掛鉤為確保條例的實施和有...
(相關資料圖)關于新版QQ的群公告在哪的知識大家了解嗎?以下就是小編整理的關于新版QQ的群公告在哪的介紹,希望可以給到大家一些參考,一起來了解下吧!新版QQ的群公告在哪?新版QQ電腦登錄時的群公告在聊天界面上有“公告”顯示;新版QQ手機登錄的群公告在群聊資料里。手機登錄找到群公告的方法如下:1、登錄打開手機QQ;2、在“聯系人”中點擊“群聊”,選擇指定的群;3、點擊該群聊天界面的右上角;4、在“群...
傳房產證下崗作廢是真的嗎?對于傳言,當地房管和土地部門已經表示,發放的房產證仍然有效。不動產登記按照“不變”的原則進行,舊證逐步被新證取代。房地產權利變更或轉讓的,發給新的產權人不動產證書;如果不變更或轉讓物權,舊證仍然合法有效,其效力與物權憑證相同,各種手續仍然可以辦理?!恫粍赢a權證書》是指國土資源部制定的房地產登記證,2015年3月1日正式實施,國土資源部制定的房地產登...