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

Java?Web中ServletContext對象詳解與應用

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

 ServletContext對象

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

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

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

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

創(chuàng)建ServletContext對象  

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

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

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

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

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

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

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

//通過 HttpServletRequest的 getServletContext方法獲取ServletContext對象
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ù)對比

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

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

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

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

數(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("今天是一個好日子" + count);
        writer.close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        doGet(req, resp);
    }
}

在瀏覽器中的操作

總結(jié) 

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

相關文章

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

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

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

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

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

    淺談Java生成唯一標識碼的三種方式

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

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

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

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

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

    springboot啟動時候報錯mongodb問題

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

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

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

    JPA添加Pageable實現(xiàn)翻頁時報錯的問題

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

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

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

    SpringBoot3使用Swagger3的示例詳解

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

最新評論

宁都县| 香港| 巍山| 沂源县| 黔西县| 河池市| 新邵县| 岢岚县| 白朗县| 丹江口市| 长乐市| 丘北县| 高台县| 永顺县| 谢通门县| 龙门县| 教育| 浦城县| 鹤山市| 南华县| 岳普湖县| 孟连| 乳源| 商河县| 灵武市| 措美县| 柳江县| 房产| 五莲县| 靖边县| 平乐县| 长白| 兴宁市| 大同市| 宜兰市| 大厂| 隆安县| 小金县| 德清县| 泰安市| 都兰县|