servlet之ServletContext簡介_動力節(jié)點Java學(xué)院整理
在對Servlet配置的web.xml文件中,經(jīng)常會使用一些初始化的參數(shù)來配置Servlet,總的功能來說就是不在Servlet程序中將某個變量寫死,而是通過外界(如web.xml文件)進(jìn)行傳遞,同時便于修改。這個是使用<servlet>標(biāo)簽下的<init-param>標(biāo)簽,使用<init-param>標(biāo)簽的<param-name>和<param-value>來封裝一個鍵值對,可以使用多個<init-param>標(biāo)簽進(jìn)行多個初始化參數(shù)的設(shè)定,我們可以看看Tomcat的web.xml中的默認(rèn)Servlet:
可以看到在這個默認(rèn)Servlet中有兩個初始化參數(shù),分別是“debug=0”和“l(fā)istings=false”。
當(dāng)Servlet在web.xml文件中配置了<init-param>標(biāo)簽后,web容器會在創(chuàng)建Servlet實例對象時,自動將這些初始化參數(shù)封裝到ServletConfig對象中,并在調(diào)用Servlet的初始化init方法時,將ServletConfig對象傳遞給Servlet。
我們從Servlet接口的初始化方法:init(ServletConfig config),可以知道,當(dāng)服務(wù)器創(chuàng)建Servlet對象就將ServletConfig對象傳遞,而在ServletConfig對象中包含著<init-param>標(biāo)簽所配置的參數(shù)和值。
剛開始學(xué)Servlet時,就已經(jīng)談到過Servlet接口的非生命周期方法就有一個方法是getServletConfig()方法,返回ServletConfig對象。所以當(dāng)我們在開發(fā)的Servlet的web.xml文件中配置一些信息:
而在Servlet中的程序獲取這個配置的參數(shù):
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig config = this.getServletConfig(); String initValue = config.getInitParameter("love"); System.out.println(initValue); }
重新部署該web應(yīng)用,然后在瀏覽器來訪問這個Servlet,將會看到在MyEclipse的控制臺上打印出:
在ServletConfig類中,getInitParameter(String name)方法是傳入特定參數(shù)名來獲取對應(yīng)參數(shù)的值,getInitParameterNames()方法則是將所有的參數(shù)名裝進(jìn)一個Enumeration對象返回,當(dāng)我們有多個參數(shù)鍵值對時:
在Servlet中進(jìn)行遍歷和輸出:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig config = this.getServletConfig(); Enumeration initParams = config.getInitParameterNames(); while(initParams.hasMoreElements()) { String paramName = (String)initParams.nextElement(); String paramValue = config.getInitParameter(paramName); System.out.println(paramName+" = "+paramValue ); } }
最后,ServletConfig對象的作用通常用于獲得編碼表類型,獲得數(shù)據(jù)庫連接信息,獲得配置文件(如Struts的web.xml文件中)等等。
說完了ServletConfig對象,當(dāng)我們?nèi)タ催@個對象的方法時會發(fā)現(xiàn)這個方法中還有一個方法getServletContext()是返回一個ServletContext對象,這是Servlet中一個非常重要的類。當(dāng)然ServletContext對象還可以從父類的方法中直接獲取。
Web容器在啟動時會為每個web應(yīng)用創(chuàng)建一個ServletContext對象,而這個ServletContext對象就代表當(dāng)前這個web應(yīng)用。因為一個ServletContext對象代表一個web應(yīng)用,所以該web應(yīng)用中所有的Servlet和其他資源都共享一個ServletContext對象,這時,我們就可以通過ServletContext對象進(jìn)行Servlet對象之間的通訊。而ServletContext對象也稱之為Context域?qū)ο蟆?nbsp;
我們先來看看ServletContext對象的獲取的兩種方式:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //兩種獲取ServletContext對象的方法: ServletContext context1 = this.getServletConfig().getServletContext(); ServletContext context2 = this.getServletContext(); //System.out.println(context1 == context2); //ture }
可以通過先獲取ServletConfig對象來獲取,或者直接通過父類的方法來獲取,這兩種方式獲取到的是同一對象(相同地址)。
既然說ServletContext代表這個web應(yīng)用,我們可以用它來進(jìn)行Servlet直接的通訊,那么我們就創(chuàng)建一個工程來進(jìn)行兩個Servlet之間的數(shù)據(jù)傳輸。在一個【myservlet】web工程下創(chuàng)建兩個Servlet:ServletDemo1和ServletDemo2,
ServletDemo1在ServletContext中設(shè)置參數(shù)鍵值對,代碼為:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); context.setAttribute("lover", "LRR"); }
ServletDemo2從ServletContext中獲取鍵值對,代碼為:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); System.out.println(context.getAttribute("lover")); }
在瀏覽器先訪問ServletDemo1后(先執(zhí)行ServletDemo1才能使ServletContext設(shè)置參數(shù)),再訪問ServletDemo2后,MyEclipse的控制臺就輸出了ServletContext中設(shè)置的參數(shù),這就達(dá)到了從一個Servlet傳遞數(shù)據(jù)給另一個Servlet。當(dāng)然這只是ServletContext的一個小小應(yīng)用。
在ServletContext類中還有g(shù)etInitParameter(String name)方法或者getInitParameterNames()方法,這兩個方法獲取的是web應(yīng)用所配置的參數(shù)(畢竟ServletContext代表web應(yīng)用),就像ServletConfig中類似的方法獲取的是某個Servlet中的<init-param>標(biāo)簽配置的參數(shù)。
而對于配置web應(yīng)用的參數(shù)是在web.xml文件中使用<context-param>標(biāo)簽,正如在該文件中為Servlet配置參數(shù)而使用<init-param>標(biāo)簽一樣。這種配置<context-param>標(biāo)簽的好處在于屬于全局性的配置,而每個Servlet的配置參數(shù)僅局限于在Servlet的范圍內(nèi),舉個例子,對于整個web應(yīng)用配置數(shù)據(jù)庫連接,這樣在web應(yīng)用中的每個Servlet都可以使用,而無需再在每個Servlet中都單獨設(shè)置一次,提高了效率。
例:在【myservlet】web工程下建立了名為ServletDemo3的Servlet,并在該web工程下的web.xml文件中添加<context-param>標(biāo)簽作為該web應(yīng)用的配置參數(shù):
在ServletDemo3中的代碼如下:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username = context.getInitParameter("username"); String password = context.getInitParameter("password"); System.out.println(username +":"+ password); }
在瀏覽器中訪問該Servlet,如果MyEclipse的控制臺能打印該信息,說明每個Servlet可以通過ServletContext對象來獲取web應(yīng)用的配置信息,也從側(cè)面說明了ServletContext代表了這個web應(yīng)用:
ServletContext類中的getMimeType(String file)方法用于返回該文件的MIME類型:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = "1.html"; ServletContext context = this.getServletContext(); System.out.println(context.getMimeType(filename)); }
輸出:text/html。
ServletContext中的轉(zhuǎn)發(fā)方法(重要)
在ServletContext對象中還有這么兩個方法:getNameDispatcher(String name)(不常用)和getRequestDispatcher(String path),返回的是RequestDispatcher對象。轉(zhuǎn)發(fā)有什么作用呢,舉個例子,比如一個Servlet中的數(shù)據(jù)交個另一個Servlet來處理,或者Servlet將某個實時數(shù)據(jù)交給JSP來顯示,雖然我們在瀏覽器中訪問的是最開始的Servlet,但是進(jìn)行轉(zhuǎn)發(fā)后看到的其他web資源,而瀏覽器的地址欄不會改變。
注:在請求對象request對象中也有這么一個getRequestDispatcher(String path)方法,功能與ServletContext對象的這個方法一樣,也可以實現(xiàn)轉(zhuǎn)發(fā),因此用哪個對象都行,沒有區(qū)別。
例:在【myservlet】web工程下創(chuàng)建一個名為ServletDemo1的Servlet和一個show.jsp,
在ServletDemo1中將數(shù)據(jù)轉(zhuǎn)發(fā)給show.jsp,代碼為:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "Ding love LRR"; this.getServletContext().setAttribute("data", data); //將數(shù)據(jù)存至web應(yīng)用的配置中 ServletContext context = this.getServletContext(); RequestDispatcher dispathcer = context.getRequestDispatcher("/show.jsp"); //通過要轉(zhuǎn)發(fā)的目的地來獲取轉(zhuǎn)發(fā)對象 dispathcer.forward(request, response); //通過forward方法將請求對象和響應(yīng)對象轉(zhuǎn)發(fā)給別人處理 }
而在show.jsp中接收這個數(shù)據(jù),并封裝在HTML中:
<font size="100px" color="red"> ${data } </font>
接著我們?nèi)g覽器里訪問ServletDemo1,就會看到:
雖然我們請求的ServletDemo1資源,但是由于在ServletDemo1中將請求進(jìn)行了轉(zhuǎn)發(fā),所以其實服務(wù)器返回的是show.jsp的資源,但是我們?yōu)g覽器地址依然會是ServletDemo1,這也是轉(zhuǎn)發(fā)和重定向的區(qū)別之一。
相關(guān)文章
Java17中record替代Lombok部分功能使用場景探究
這篇文章主要介紹了使用Java17中的record替代Lombok的部分功能,本文來為大家小小的總結(jié)下,我們可以在哪些地方,利用record來替換Lombok2024-01-01mybatis@insert?注解如何判斷insert或是update
這篇文章主要介紹了mybatis@insert?注解如何判斷insert或是update,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07IDEA 顯示Run Dashboard窗口的2種方式(推薦)
這篇文章主要介紹了IDEA 顯示Run Dashboard窗口的2種方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08java實現(xiàn)合并兩個已經(jīng)排序的列表實例代碼
這篇文章主要介紹了java實現(xiàn)合并兩個已經(jīng)排序的列表實例代碼,有需要的朋友可以參考一下2013-12-12java8中NIO緩沖區(qū)(Buffer)的數(shù)據(jù)存儲詳解
在本篇文章中小編給大家分享了關(guān)于java8中NIO緩沖區(qū)(Buffer)的數(shù)據(jù)存儲的相關(guān)知識點,需要的朋友們參考下。2019-04-04