Servlet中的getServletContext()方法詳解

public class ServletContext01 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.獲取對象
        ServletContext context = getServletContext();
        String address = context.getInitParameter("address");
        System.out.println(address);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
web.xml參數設置:
 <context-param>
    <param-name>address</param-name>
    <param-value>中國深圳</param-value>
  </context-param>
這個是一個比較經典的doGet和doPost方法的案例,繼承了httpservlet類,
我以爲可能剛接觸的人應該是 ServletContext context = getServletContext();這句話不是很懂,其實咱們能夠這麼理解,一個servlet可使用getservletcontext()方法獲得了web應用中的servletcontext,從而使用servletcontext接口的一些方法:好比咱們能夠看到後面的那句話String address = context.getInitParameter("address");實際上就是用了servletcontext接口裏面的getInitParameter()方法:而address就是參數的名稱,得到的值爲中國深圳,因此有沒有以爲這樣很方便?
------------------------------------------------------------------------------------------------------------------------------------------------------------
剛在另外一篇博客裏面有看到他對於getservletcontext()方法的解釋,可能比較文字古板,(不想看的能夠跳過這段,我以爲不忙的話看看也挺好的)
一個servlet上下文是servlet引擎提供用來服務於Web應用的接口。Servlet上下文具備名字(它屬於Web應用的名字)惟一映射到文件系統的一個目錄。
一個servlet能夠經過ServletConfig對象的getServletContext()方法獲得servlet上下文的引用,若是servlet直接或間接調用子類GenericServlet,則可使用getServletContext()方法。
Web應用中servlet可使用servlet上下文獲得:
1.在調用期間保存和檢索屬性的功能,並與其餘servlet共享這些屬性。
2.讀取Web應用中文件內容和其餘靜態資源的功能。
3.互相發送請求的方式。
4.記錄錯誤和信息化消息的功能。
-------------------------------------------------------------------------------------------------------------------------------------------------------------
 
下面總結servletcontext接口裏面的方法:
1.String getInitParameter(String name)    返回指定上下文範圍的初始化參數值。
2.Object getAttribute(String name)    返回servlet上下文中具備指定名字的對象,或使用已指定名捆綁一個對象。從Web應用的標準觀點看,這樣的對象是全局對象,由於它們能夠被同一servlet在另外一時刻訪問。或上下文中任意其餘servlet訪問。
這個方法我用過例子:(我在作計數網站曾經被登陸過幾回用過這個方法):
下面是個人程序:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        //1.獲取數據
        String userName = request.getParameter("username");        //httpservletrequest裏面的方法。獲取請求頭中的參數,
        String password = request.getParameter("password");
//        System.out.println("userName="+userName+"password="+password);
       
        //2.校驗數據
       
        PrintWriter pw = response.getWriter();    //用了這個輸出流能夠直接將文字寫在瀏覽器上       
       
       
        if("admin".equals(userName) && "123".equals(password)) {
            pw.write("login success.....");
            //若是成功的話,就跳轉到login_success.html那裏
           
            //成功的次數累加:
            //保留以前存的值,而後在舊的值的基礎上+1
            Object obj = getServletContext().getAttribute("count");        //getAttribute是servlet裏面的方法
           
           
            //默認就是0次
            int totalCount = 0;
            if(obj != null) {
                totalCount = (int)obj;
            }
           
            System.out.println("已登陸成功的次數是:"+totalCount);
           
           
            //給這個count賦新的值
            getServletContext().setAttribute("count", totalCount+1);
           
           
            //跳到成功的頁面
            //設置狀態碼,進行 從新定位狀態碼
           
           
            response.setStatus(302);
           
            //定位跳轉的位置是哪個頁面
            response.setHeader("Location", "login_success.html");
           
        } else {
            pw.write("login failed.....");
        }
       
    }
3.String getRealPath(String path)    給定一個URI,返回文件系統中URI對應的絕對路徑。若是不能進行映射,返回null。
下面是我寫過的程序代碼:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        //獲取servletcontext對象
        ServletContext context = this.getServletContext();
        //獲取給定的文件在服務器上面的絕對路徑
        String path = context.getRealPath("");
       
        System.out.println(path);    
 
這個實際上輸出的就是文件在電腦中的絕對路徑,也就是讀取web中的資源文件;
 
3.void setAttribute(String name,Object obj)    設置servlet上下文中具備指定名字的對象。
 
下面幾個我還沒用過,我用了之後有時間再補充吧:
4.Enumeration getAttributeNames()    返回保存在servlet上下文中全部屬性名字的枚舉。      
5.ServletContext getContext(String uripath)    返回映射到另外一URL的servlet上下文。在同一服務器中URL必須是以「/」開頭的絕對路徑。
6.Enumeration getInitParameterNames()    返回(可能爲空)指定上下文範圍的初始化參數值名字的枚舉值。       7.int getMajorVersion()    返回此上下文中支持servlet API級別的最大和最小版本號。       8.int getMinorVersion()           9.String getMimeType(String fileName)    返回指定文件名的MIME類型。典型狀況是基於文件擴展名,而不是文件自己的內容(它能夠沒必要存在)。若是MIME類型未知,能夠返回null。       10.RequestDispatcher getNameDispatcher(String name)    返回具備指定名字或路徑的servlet或JSP的RequestDispatcher。若是不能建立RequestDispatch,返回null。若是指定路徑,必須心「/」開頭,而且是相對於servlet上下文的頂部。       11.RequestDispatcher getNameDispatcher(String path)           13.URL getResource(String path)    返回相對於servlet上下文或讀取URL的輸入流的指定絕對路徑相對應的URL,若是資源不存在則返回null。       14.InputStream getResourceAsStream(String path)           15.String getServerInfo()    返順servlet引擎的名稱和版本號。       16.void log(String message) 17.void log(String message,Throwable t)    將一個消息寫入servlet註冊,若是給出Throwable參數,則包含棧軌跡。       18.void removeAttribute(String name)    從servlet上下文中刪除指定屬性。
相關文章
相關標籤/搜索