最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?Web中ServletContext對(duì)象詳解與應(yīng)用

 更新時(shí)間:2023年04月26日 10:32:02   作者:Lungcen  
ServletContext是一個(gè)容器,可以用來存放變量,供一個(gè)web項(xiàng)目中多個(gè)Servlet共享,下面這篇文章主要給大家介紹了關(guān)于Java?Web中ServletContext對(duì)象詳解與應(yīng)用的相關(guān)資料,需要的朋友可以參考下

 ServletContext對(duì)象

Web 應(yīng)用中的所有 Servlet 共享同一個(gè) ServletContext 對(duì)象,不同 Servlet 之間可以通過 ServletContext 對(duì)象實(shí)現(xiàn)數(shù)據(jù)通訊,因此 ServletContext 對(duì)象也被稱為 Context 域?qū)ο蟆?/strong>

域?qū)ο笫欠?wù)器在內(nèi)存上創(chuàng)建的存儲(chǔ)空間,該空間用于不同動(dòng)態(tài)資源(例如 Servlet、JSP)之間傳遞與共享數(shù)據(jù)。

 獲取上下文初始化參數(shù)的相關(guān)方法

StringgetInitParameter(String name)根據(jù)初始化參數(shù)名 name,返回對(duì)應(yīng)的初始化參 數(shù)值。
EnumerationgetInitParameterNames()返回 Web 應(yīng)用所有上下文初始化參數(shù)名的枚舉 集合,如果沒有上下文初始化參數(shù),則返回一個(gè)空的枚舉集合。

創(chuàng)建ServletContext對(duì)象  

1)通過 GenericServlet 提供的 getServletContext() 方法

//通過 GenericServlet的getServletContext方法獲取ServletContext對(duì)象
ServletContext servletContext = this.getServletContext();

2)通過 ServletConfig 提供的 getServletContext() 方法

//通過 ServletConfig的 getServletContext方法獲取ServletContext對(duì)象
ServletContext servletContext = this.getServletConfig().getServletContext();
//通過 Config的 getServletContext方法獲取ServletContext對(duì)象
ServletContext context = config.getServletContext();

3)通過 HttpSession 提供的 getServletContext() 方法

//通過 Session的 getServletContext方法獲取ServletContext對(duì)象
ServletContext context = req.getSession().getServletContext();

4)通過 HttpServletRequest 提供的 getServletContext() 方法

//通過 HttpServletRequest的 getServletContext方法獲取ServletContext對(duì)象
ServletContext servletContext = req.getServletContext();

上下文初始化參數(shù)

局部參數(shù)

    <servlet>
        <init-param>
            <param-name>name</param-name>
            <param-value>Lungcen</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>110120130</param-value>
        </init-param>
    </servlet>

全局參數(shù)

    <context-param>
        <param-name>姓名</param-name>
        <param-value>Lungcen</param-value>
    </context-param>
    <context-param>
        <param-name>年齡</param-name>
        <param-value>19</param-value>
    </context-param>

 獲取ServletContext的全局參數(shù)

Enumeration<String> names = this.context.getInitParameterNames();
        
        while (names.hasMoreElements())
        {
            String s = names.nextElement();
            writer.write(s + "->" + context.getInitParameter(s) + "<br/>");
        }

ServletContext 屬性與上下文初始化參數(shù)對(duì)比

不 同 點(diǎn)ServletContext 的屬性上下文初始化參數(shù)
創(chuàng) 建 方 式ServletContext 的屬性通過調(diào)用 ServletContext 接口的 setAttribute() 方法 創(chuàng)建上下文初始化參數(shù)通過 web.xml 使用 元素配置
可 進(jìn) 行 的 操 作ServletContext 的屬性可以通過 ServletContext 接口的方法進(jìn)行讀取、新 增、修改、移除等操作上下文初始化參數(shù)在容器啟動(dòng)后只能被 讀取,不能進(jìn)行新增、修改和移除操作
生 命 周 期ServletContext 中屬性的生命周期從創(chuàng)建開 始,到該屬性被移除(remove)或者容器關(guān) 閉結(jié)束上下文初始化參數(shù)的生命周期,從容器 啟動(dòng)開始,到 Web 應(yīng)用被卸載或容器 關(guān)閉結(jié)束
作 用使用 ServletContext 中的屬性可以實(shí)現(xiàn) Servlet 之間的數(shù)據(jù)通訊使用上下文初始化參數(shù)無法實(shí)現(xiàn)數(shù)據(jù)通訊

實(shí)現(xiàn)數(shù)據(jù)通訊

在 Servlet 中,調(diào)用 ServletContext 接口的 setAttribute() 方法可以創(chuàng)建一些屬性,這些屬性被存 放在 ServletContext 對(duì)象中。應(yīng)用中所有 Servlet 都可以對(duì)這些屬性進(jìn)行訪問和操作,通過它們可以實(shí)現(xiàn)應(yīng)用內(nèi)不同 Servlet 之間的數(shù)據(jù)通訊。

voidsetAttribute(String name, Object object)把一個(gè) Java 對(duì)象與一個(gè)屬性名綁定,并將它作為一個(gè)屬 性存放到 ServletContext 中。 參數(shù) name 為屬性名,參數(shù) object 為屬性值。
voidremoveAttribute(String name)從 ServletContext 中移除屬性名為 name 的屬性。
ObjectgetAttribute(String name)根據(jù)指定的屬性名 name,返回 ServletContext 中對(duì)應(yīng) 的屬性值。

數(shù)據(jù)通訊的程序?qū)嵗?/h3>
package com.zpark.servlet;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
@WebServlet(urlPatterns = "/LLL.do")
public class MyServlet04 extends HttpServlet {
 
    @Override
    public void init() throws ServletException {
        getServletContext().setAttribute("count", 0);
    }
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Integer count = (Integer) getServletContext().getAttribute("count");
        count++;
        getServletContext().setAttribute("count", count);
 
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write("歡迎來到界面" + count);
        writer.close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        doGet(req, resp);
    }
}
package com.zpark.servlet;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
@WebServlet("/Lun5.do")
public class MyServlet05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        Integer count = (Integer) getServletContext().getAttribute("count");
        writer.write("今天是一個(gè)好日子" + count);
        writer.close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        doGet(req, resp);
    }
}

在瀏覽器中的操作

總結(jié) 

到此這篇關(guān)于Java Web中ServletContext對(duì)象詳解與應(yīng)用的文章就介紹到這了,更多相關(guān)Java Web ServletContext對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java反射如何獲取字段屬性值

    Java反射如何獲取字段屬性值

    這篇文章主要介紹了Java反射如何獲取字段屬性值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • jdk21安裝后無jre文件該如何解決

    jdk21安裝后無jre文件該如何解決

    java開發(fā)少不了安裝jdk,下面這篇文章主要給大家介紹了關(guān)于jdk21安裝后無jre文件該如何解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • 淺談Java生成唯一標(biāo)識(shí)碼的三種方式

    淺談Java生成唯一標(biāo)識(shí)碼的三種方式

    我們經(jīng)常會(huì)遇到這樣的場(chǎng)景,需要生成一個(gè)唯一的序列號(hào)來表明某一個(gè)數(shù)據(jù)的唯一性,本文主要介紹了淺談Java生成唯一標(biāo)識(shí)碼的三種方式,感興趣的可以來了解一下
    2022-01-01
  • resty mail的簡(jiǎn)單發(fā)送郵件方法

    resty mail的簡(jiǎn)單發(fā)送郵件方法

    這篇文章主要為大家介紹了簡(jiǎn)單的resty mail發(fā)送郵件方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • 最安全的加密算法Bcrypt防止數(shù)據(jù)泄露詳解

    最安全的加密算法Bcrypt防止數(shù)據(jù)泄露詳解

    這篇文章主要為大家介紹了最安全的加密算法Bcrypt防止數(shù)據(jù)泄露詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題

    springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題

    這篇文章主要介紹了springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 淺談String類型如何轉(zhuǎn)換為time類型存進(jìn)數(shù)據(jù)庫

    淺談String類型如何轉(zhuǎn)換為time類型存進(jìn)數(shù)據(jù)庫

    這篇文章主要介紹了String類型如何轉(zhuǎn)換為time類型存進(jìn)數(shù)據(jù)庫,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • JPA添加Pageable實(shí)現(xiàn)翻頁時(shí)報(bào)錯(cuò)的問題

    JPA添加Pageable實(shí)現(xiàn)翻頁時(shí)報(bào)錯(cuò)的問題

    這篇文章主要介紹了解決JPA添加Pageable實(shí)現(xiàn)翻頁時(shí)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • jdk15的安裝與配置全過程記錄

    jdk15的安裝與配置全過程記錄

    這篇文章主要給大家介紹了關(guān)于jdk15的安裝與配置,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • SpringBoot3使用Swagger3的示例詳解

    SpringBoot3使用Swagger3的示例詳解

    本文介紹了如何在Spring Boot 3項(xiàng)目中使用Swagger3進(jìn)行后端接口的前端展示,首先,通過添加依賴并配置application.yml文件來快速啟動(dòng)Swagger,然后,詳細(xì)介紹了Swagger3的新注解與Swagger2的區(qū)別,并提供了一些常用注解的使用示例,感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評(píng)論

芦山县| 固镇县| 宝山区| 方山县| 洪江市| 江永县| 庄河市| 拜城县| 平武县| 河北省| 鄂伦春自治旗| 仁布县| 彰化市| 永仁县| 丹凤县| 犍为县| 温州市| 澄迈县| 涟源市| 普宁市| 郁南县| 平利县| 合肥市| 许昌市| 邢台市| 陕西省| 正宁县| 北碚区| 龙门县| 桐柏县| 肇东市| 东辽县| 双鸭山市| 文水县| 元阳县| 拜城县| 那曲县| 昆明市| 阳曲县| 安多县| 襄汾县|