13_ServletContext對象


【簡介】

ServletContext即Servlet上下文對象,該對象表示當前的web應用環境信息,一個Web應用只會創建一個ServletContext對象。

Web容器啟動的時候,它會為每個Web應用程序都創建一個對應的ServletContext對象,它代表當前的web應用。

[注意]

由於一個Web應用中的所有Servlet共享一個ServletContext對象,所以多個Servlet通過ServletContext對象實現數據共享,

ServletContext對象通常稱為Context對象。

【ServletContext創建時機】

ServletContext對象是在TomCat服務器加載完當前Web應用后創建出來的。

ServletContext對象是作為ServletConfig對象成員變量傳入Servlet中。

通過ServletConfiggetServletContext()方法就可以得到ServletContext對象。

看下ServletConfig中相關的ServletContext代碼:

class ServletConfig{      //ServletConfig對象中維護了ServletContext對象的應用 ServletContext context;
    getInitParameter();
    getInitParameterNames();
    public ServletContext getServletContext(){     //返回一個ServletContext對象
      return contex;
    }
}              

在Servet中的init的方法實例化一個ServletConfig

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
}

[ 注意 ]

this.ServletConfig.getServletContext():通過ServletConfig對象來獲取ServletContext對象。

ServletContext對象:啟動時創建

ServletConfig對象:調用init方法之前創建的,在ServletContext對象之前。

 

【在Servlet中得到ServletContext的兩種方式】

 

 

 

【ServletContext的5大作用】

1.獲取web的上下文路徑

  String getContextPath();

2.獲取全局的參數

  String getInitParameter(String name);

  Enumeration getInitParameterNames();

3.和域對象相關的

  void setAttribute(String name,Onject object);

  Object getAttribute(String name);

  void removeAttribute(String name);

  域對象(域對象就是在不同資源之前來共享數據,保存數據,獲取數據)

  ServletContext是我們學習的第一個域對象(Servlet共有三個域對象ServletContext、HttpServletRequest、HttpSession)

4. 請求轉發的

  RequestDispatcher getRequestDispatcher(String path);

  在Servlet跳轉頁面:

  4.1請求重定向(你找我借錢,我沒有,你自己去找他借錢)

    1.地址欄會改變,變成重定向到的地址

    2.可以跳轉到項目內的資源,也可以跳轉項目外的資源

    3.瀏覽器向服務器發出兩次請求,那么不能使用請求來作為域對象來共享數據。

  4.2請求轉發(你找我借錢,我沒有,我幫你去向他借錢)

    1.地址欄不會改變

    2.只能跳轉到項目內的資源,不能跳轉項目外的資源。

    3.瀏覽器向服務器發出一次請求,那么可以使用請求作為域對象共享數據。

5.讀取web項目的資源文件

  String getRealPath(String path);

  InputStream getResourceAsStream(String path);

  URL getResource(String path);

 

【利用ServletContext對象來收發數據(Servlet3.0新特性)】

利用ServletContext實現SendServlet和ReceiveServlet之間的數據共享

【SendServlet.java 發數據】

package com.Higgin.context;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SendServlet")
public class SendServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data="Higgin";
        this.getServletContext().setAttribute("data", data);
        System.out.println("SendServlet發送的數據為:"+data);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    }

}

【ReceiveServlet.java 收數據】

package com.Higgin.context;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ReceiveServlet") public class ReceiveServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data=(String) this.getServletContext().getAttribute("data");
        System.out.println("ReceiveServlet接收到的數據:"+data);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

[ 發送和接收數據(使用不同的瀏覽器,應用場景:聊天室) ]

 

 

【通過ServletContext讀取資源文件db.properties】

【工程截圖】

[ db.properties ]

url=jdbc:mysql://localhost:3306/test
username=root password=123456

[ ServletDemo00.java ]

package com.Higgin.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ServletDemo00") public class ServletDemo00 extends HttpServlet {
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        
        InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties props=new Properties();  
        props.load(in);

        String url=props.getProperty("url");
        String username=props.getProperty("username");
        String password=props.getProperty("password");
        
        System.out.println("url=="+url);
        System.out.println("username=="+username);
        System.out.println("password=="+password);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }

}

[ 運行結果 ]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM