ServletContext類如何在servlet中使用-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)ServletContext類如何在servlet中使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司主營云浮網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,云浮h5重慶小程序開發(fā)搭建,云浮網(wǎng)站營銷推廣歡迎云浮等地區(qū)企業(yè)咨詢

在對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:

ServletContext類如何在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í)例對象時,自動將這些初始化參數(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文件中配置一些信息:

ServletContext類如何在servlet中使用

而在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的控制臺上打印出:

ServletContext類如何在servlet中使用

在ServletConfig類中,getInitParameter(String name)方法是傳入特定參數(shù)名來獲取對應(yīng)參數(shù)的值,getInitParameterNames()方法則是將所有的參數(shù)名裝進(jìn)一個Enumeration對象返回,當(dāng)我們有多個參數(shù)鍵值對時:

ServletContext類如何在servlet中使用

在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 );
    }
  }

ServletContext類如何在servlet中使用

最后,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)用。因?yàn)橐粋€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中都單獨(dú)設(shè)置一次,提高了效率。

例:在【myservlet】web工程下建立了名為ServletDemo3的Servlet,并在該web工程下的web.xml文件中添加<context-param>標(biāo)簽作為該web應(yīng)用的配置參數(shù):

ServletContext類如何在servlet中使用

在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類如何在servlet中使用

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í)時數(shù)據(jù)交給JSP來顯示,雖然我們在瀏覽器中訪問的是最開始的Servlet,但是進(jìn)行轉(zhuǎn)發(fā)后看到的其他web資源,而瀏覽器的地址欄不會改變。

注:在請求對象request對象中也有這么一個getRequestDispatcher(String path)方法,功能與ServletContext對象的這個方法一樣,也可以實(shí)現(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,就會看到:

ServletContext類如何在servlet中使用

雖然我們請求的ServletDemo1資源,但是由于在ServletDemo1中將請求進(jìn)行了轉(zhuǎn)發(fā),所以其實(shí)服務(wù)器返回的是show.jsp的資源,但是我們?yōu)g覽器地址依然會是ServletDemo1,這也是轉(zhuǎn)發(fā)和重定向的區(qū)別之一。

關(guān)于ServletContext類如何在servlet中使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享標(biāo)題:ServletContext類如何在servlet中使用-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article46/cddshg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、服務(wù)器托管網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營