JaveWeb學習之Servlet(二):ServletConfig和ServletContext

原文同步發表至我的博客【夜月歸途】html

原文連接:http://www.guitu18.com/se/java/2018-07-26/20.htmljava

ServletConfig

繼續查看ServletConfig源碼:web

package javax.servlet;
public interface ServletConfig {
    public String getServletName();
    public ServletContext getServletContext();
    public String getInitParameter(String name);
    public Enumeration<String> getInitParameterNames();
}

 

getServletName();

getServletName方法用於獲取當前Servlet名稱,這個是在xml中配置的:<servlet-name>springmvcdemo</servlet-name>;spring

getServletContext();

getServletContext方法用於獲取表明當前web應用的ServletContext對象;mvc

getInitParameter(String name);

getInitParameter方法用於獲取當前Servlet指定名稱的初始化參數的值;app

getInitParameterNames();

getInitParameterNames獲取當前Servlet全部初始化參數的名字枚舉集合;dom

Servlet容器初始化時每個Servlet對象時,會爲其建立一個ServletConfig對象,經過Servlet的init方法,會把ServletContext對象傳遞給當前Servlet:測試

public void init(ServletConfig config) throws ServletException;

 

以後使用this.getServletConfig()對象獲取當前Servlet的ServletConfig對象;ui

ServletConfig servletConfig = getServletConfig();

 

咱們在xml中配置Servlet時,還能夠配置Servlet初始化參數信息:this

<servlet>
    <servlet-name>Servlet_1</servlet-name>
    <servlet-class>com.guitu.servlet.Servlet_1</servlet-class>
    <init-param>
        <param-name>username</param-name>
        <param-value>zhangkuan</param-value>
    </init-param>
</servlet>

 

以後能夠經過servletConfig獲取InitParameter參數;

Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();

 

得到的是一個參數名字的枚舉集合,在調用getInitParameter(String name)便可得到參數的值:

String initParameter = servletConfig.getInitParameter(parameterName);

 

ServletContext

Servlet容器啓動後,會爲每個獨立的Web應用建立一個ServletContext實例,表明當前Web應用;
ServletContext由全部Servlet共享,因此ServletContext中的參數全部Servlet都可以獲取,多個web組件之間使用它實現數據共享;

ServletContext經常使用方法:

getAttribute(String name);

獲取ServletContext中存儲的數據;

setAttribute(String name, Object obj);

向ServletContext中存儲數據;

getInitParameter(String name);

獲取<context-param>標籤中爲整個Web應用配置的初始化參數;

getInitParameterNames();

一次性獲取<context-param>裏全部的初始化參數名的枚舉;

getRealPath(String path);

獲取應用程序內指定資源的絕對路徑;

getResource(String parh);

path必須是/開頭,表明當前web應用程序的根目錄。返回一個表明某個資源的URL對象;

getResoutceAsStream(String parh);

可使用相對於根目錄的路徑訪問到web目錄下的全部文件,而沒必要知道絕對路徑,返回文件流;

ServletConfig、ServletContext測試Demo

一、Servlet_1:

package com.guitu.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet_1 extends HttpServlet {

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

        ServletConfig servletConfig = getServletConfig();
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        System.out.println("=====Servlet_1.ServletConfig.InitParameter=====");
        while (initParameterNames.hasMoreElements()) {
            String parameterName = initParameterNames.nextElement();
            String initParameter = servletConfig.getInitParameter(parameterName);
            System.out.println(parameterName + ":" + initParameter);
        }

        System.out.println("=====Servlet_1.ServletContext.InitParameter=====");
        ServletContext servletContext = getServletContext();
        Enumeration<String> contextInitParameters = servletContext.getInitParameterNames();
        while (contextInitParameters.hasMoreElements()) {
            String parameterName = contextInitParameters.nextElement();
            String initParameter = servletContext.getInitParameter(parameterName);
            System.out.println(parameterName + ":" + initParameter);
        }

        servletContext.setAttribute("message", "Hello World !");

    }

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

}

 

二、Servlet_2:

package com.guitu.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet_2 extends HttpServlet {

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

        ServletConfig servletConfig = getServletConfig();
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        System.out.println("=====Servlet_2.ServletConfig.InitParameter======");
        while (initParameterNames.hasMoreElements()) {
            String parameterName = initParameterNames.nextElement();
            String initParameter = servletConfig.getInitParameter(parameterName);
            System.out.println(parameterName + ":" + initParameter);
        }

        System.out.println("=====Servlet_2.ServletContext.InitParameter=====");
        ServletContext servletContext = getServletContext();
        Enumeration<String> contextInitParameters = servletContext.getInitParameterNames();
        while (contextInitParameters.hasMoreElements()) {
            String parameterName = contextInitParameters.nextElement();
            String initParameter = servletContext.getInitParameter(parameterName);
            System.out.println(parameterName + ":" + initParameter);
        }

        System.out.println("=====servletContext.getAttribute=====");
        Object object = servletContext.getAttribute("message");
        System.out.println("message:" + object);
    }

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

}

 

三、web.xml配置:

<context-param>
    <param-name>domain</param-name>
    <param-value>guitu18.com</param-value>
</context-param>
<context-param>
    <param-name>name</param-name>
    <param-value>夜月歸途</param-value>
</context-param>
<servlet>
    <servlet-name>Servlet_1</servlet-name>
    <servlet-class>com.guitu.servlet.Servlet_1</servlet-class>
    <init-param>
        <param-name>username</param-name>
        <param-value>zhangkuan</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Servlet_1</servlet-name>
    <url-pattern>/Servlet_1</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>Servlet_2</servlet-name>
    <servlet-class>com.guitu.servlet.Servlet_2</servlet-class>
    <init-param>
        <param-name>password</param-name>
        <param-value>Guitu18</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Servlet_2</servlet-name>
    <url-pattern>/Servlet_2</url-pattern>
</servlet-mapping>

 

在Servlet1和Servlet2中,分別得到了ServletConfig對象和ServletContext對象;
在web.xml中,也分別爲他們配置了不一樣的初始化參數,同時配置了一個context級的初始化參數;
以後分別在兩個Servlet中獲取ServletConfig和ServletContext的InitParamer參數;
在Servlet1中還經過setAttribute向ServletContext域中保存了一條消息message,在Servlet2中,即可以經過getAttribute獲取這條消息;

四、訪問兩個Servlet,先訪問Servlet_1:

=====Servlet_1.ServletConfig.InitParameter=====
username:zhangkuan
=====Servlet_1.ServletContext.InitParameter=====
domain:guitu18.com
name:夜月歸途

 

再訪問Servlet_2:

=====Servlet_2.ServletConfig.InitParameter=====
password:Guitu18
=====Servlet_2.ServletContext.InitParameter=====
domain:guitu18.com
name:夜月歸途
=====servletContext.getAttribute=====
message:Hello World !

 

能夠看到,每一個Servlet擁有本身ServletConfig對象,數據不互通,而ServletContext中的初始化參數和context域中的參數,是全部Servlet共享的,可使用context域實現數據共享;

相關文章
相關標籤/搜索