日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

(JavaWeb)ServletContext对象

發(fā)布時(shí)間:2025/3/20 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (JavaWeb)ServletContext对象 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • ServletContext
    • 1.共享數(shù)據(jù)
    • 2.獲取初始化參數(shù)
    • 3.請求轉(zhuǎn)發(fā)
    • 4.讀取資源文件

ServletContext

  • web容器在啟動(dòng)的時(shí)候,它會(huì)為每個(gè)web程序都創(chuàng)建一個(gè)對應(yīng)的ServletContext對象,它代表了當(dāng)前的web應(yīng)用;

1.共享數(shù)據(jù)

Servlet1可以將數(shù)據(jù)存放在ServletContext中,Servlet2和Servlet3可以在ServletContext中取得Servlet1在ServletContext中存放的數(shù)據(jù)。

在HelloServlet中存放String數(shù)據(jù) 伊澤瑞爾。

public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//獲取servletContext對象ServletContext servletContext = this.getServletContext();String name = "伊澤瑞爾";//以鍵值對的形式將數(shù)據(jù)放入servletContext對象中servletContext.setAttribute("name",name);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

在GetNameServlet中取得數(shù)據(jù)并顯示在網(wǎng)站上

public class GetNameServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//獲取ServletContext對象ServletContext servletContext = this.getServletContext();//獲取HelloServlet中在獲取ServletContext對象中存放的數(shù)據(jù)String name = (String) servletContext.getAttribute("name");//設(shè)置響應(yīng)信息為中文resp.setContentType("text/html");resp.setCharacterEncoding("utf-8");resp.getWriter().write(name);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

先訪問HelloServlet頁面將數(shù)據(jù)存放在ServletContext中,再訪問GetNameServlet拿出數(shù)據(jù)并顯示在網(wǎng)頁。

2.獲取初始化參數(shù)

<!--配置一些web應(yīng)用初始化參數(shù)--><context-param><param-name>url</param-name><param-value>jdbc:mysql://localhost:3306/mybatis</param-value></context-param> protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();String url = context.getInitParameter("url");resp.getWriter().print(url); }

3.請求轉(zhuǎn)發(fā)

public class ServletDemo01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//創(chuàng)建servletContext對象ServletContext servletContext = this.getServletContext();//請求轉(zhuǎn)發(fā)servletContext.getRequestDispatcher("/getName").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

運(yùn)行,顯示的是GetNameServlet的頁面,這里null是由于沒有訪問hello的緣故。

顯示的是GetNameServlet的頁面,但是路徑卻還是自己的。

如圖,A向B請求資源,B向C請求資源,B將C返回的資源再返回給A,A自始至終沒有接觸C,所以顯示的還是B的路徑。

4.讀取資源文件

Properties

  • 在resources目錄下新建db.properties文件
username=ez password=123456 public class ServletDemo02 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//創(chuàng)建servletContext對象ServletContext servletContext = this.getServletContext();//獲取properties文件的字節(jié)輸入流對象InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");//創(chuàng)建properies對象Properties prop = new Properties();//加載流prop.load(is);String user = prop.getProperty("username");String pwd = prop.getProperty("password");//顯示resp.getWriter().write(user+":"+pwd);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

總結(jié)

以上是生活随笔為你收集整理的(JavaWeb)ServletContext对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。