Java?Web中ServletContext對(duì)象詳解與應(yīng)用
ServletContext對(duì)象
Web 應(yīng)用中的所有 Servlet 共享同一個(gè) ServletContext 對(duì)象,不同 Servlet 之間可以通過(guò) 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)方法
String | getInitParameter(String name) | 根據(jù)初始化參數(shù)名 name,返回對(duì)應(yīng)的初始化參 數(shù)值。 |
Enumeration | getInitParameterNames() | 返回 Web 應(yīng)用所有上下文初始化參數(shù)名的枚舉 集合,如果沒(méi)有上下文初始化參數(shù),則返回一個(gè)空的枚舉集合。 |
創(chuàng)建ServletContext對(duì)象
1)通過(guò) GenericServlet 提供的 getServletContext() 方法
//通過(guò) GenericServlet的getServletContext方法獲取ServletContext對(duì)象 ServletContext servletContext = this.getServletContext();
2)通過(guò) ServletConfig 提供的 getServletContext() 方法
//通過(guò) ServletConfig的 getServletContext方法獲取ServletContext對(duì)象 ServletContext servletContext = this.getServletConfig().getServletContext();
//通過(guò) Config的 getServletContext方法獲取ServletContext對(duì)象 ServletContext context = config.getServletContext();
3)通過(guò) HttpSession 提供的 getServletContext() 方法
//通過(guò) Session的 getServletContext方法獲取ServletContext對(duì)象 ServletContext context = req.getSession().getServletContext();
4)通過(guò) HttpServletRequest 提供的 getServletContext() 方法
//通過(guò) 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 的屬性通過(guò)調(diào)用 ServletContext 接口的 setAttribute() 方法 創(chuàng)建 | 上下文初始化參數(shù)通過(guò) web.xml 使用 元素配置 |
可 進(jìn) 行 的 操 作 | ServletContext 的屬性可以通過(guò) ServletContext 接口的方法進(jìn)行讀取、新 增、修改、移除等操作 | 上下文初始化參數(shù)在容器啟動(dòng)后只能被 讀取,不能進(jìn)行新增、修改和移除操作 |
生 命 周 期 | ServletContext 中屬性的生命周期從創(chuàng)建開(kāi) 始,到該屬性被移除(remove)或者容器關(guān) 閉結(jié)束 | 上下文初始化參數(shù)的生命周期,從容器 啟動(dòng)開(kāi)始,到 Web 應(yīng)用被卸載或容器 關(guān)閉結(jié)束 |
作 用 | 使用 ServletContext 中的屬性可以實(shí)現(xiàn) Servlet 之間的數(shù)據(jù)通訊 | 使用上下文初始化參數(shù)無(wú)法實(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)行訪問(wèn)和操作,通過(guò)它們可以實(shí)現(xiàn)應(yīng)用內(nèi)不同 Servlet 之間的數(shù)據(jù)通訊。
void | setAttribute(String name, Object object) | 把一個(gè) Java 對(duì)象與一個(gè)屬性名綁定,并將它作為一個(gè)屬 性存放到 ServletContext 中。 參數(shù) name 為屬性名,參數(shù) object 為屬性值。 |
void | removeAttribute(String name) | 從 ServletContext 中移除屬性名為 name 的屬性。 |
Object | getAttribute(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("歡迎來(lái)到界面" + 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);
}
}
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("歡迎來(lái)到界面" + 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異常處理之try...catch...finally詳解
今天小編就為大家分享一篇關(guān)于Java異常處理之try...catch...finally詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01spring?cloud中Feign導(dǎo)入jar失敗的問(wèn)題及解決方案
這篇文章主要介紹了spring?cloud中Feign導(dǎo)入jar失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03RabbitMQ 的七種隊(duì)列模式和應(yīng)用場(chǎng)景
最近學(xué)習(xí)RabbitMQ,本文就記錄一下RabbitMQ 的七種隊(duì)列模式和應(yīng)用場(chǎng)景,方便以后使用,也方便和大家共享,相互交流2021-05-05MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join)的功能實(shí)現(xiàn)
mybatis-plus作為mybatis的增強(qiáng)工具,簡(jiǎn)化了開(kāi)發(fā)中的數(shù)據(jù)庫(kù)操作,這篇文章主要介紹了MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join),需要的朋友可以參考下2022-08-08Java web基礎(chǔ)學(xué)習(xí)之開(kāi)發(fā)環(huán)境篇(詳解)
下面小編就為大家?guī)?lái)一篇Java web基礎(chǔ)學(xué)習(xí)之開(kāi)發(fā)環(huán)境篇(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Spring Cloud Gateway不同頻率限流的解決方案(每分鐘,每小時(shí),每天)
SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了Spring Cloud Gateway不同頻率限流(每分鐘,每小時(shí),每天),需要的朋友可以參考下2020-10-10Java面向?qū)ο箢?lèi)和對(duì)象實(shí)例詳解
面向?qū)ο竽耸荍ava語(yǔ)言的核心,是程序設(shè)計(jì)的思想,這篇文章主要介紹了Java面向?qū)ο箢?lèi)和對(duì)象的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03APP轉(zhuǎn)盤(pán)抽獎(jiǎng)Java服務(wù)端接口詳解
這篇文章主要為大家詳細(xì)介紹了APP轉(zhuǎn)盤(pán)抽獎(jiǎng)Java服務(wù)端接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01