天天看點

Servlet中的ServletContext接口

For every

Web application

a

ServletContext

object is created by the web container. ServletContext object is used to get configuration information from

Deployment Descriptor

(web.xml) which will be available to any servlet or JSPs that are part of the web app.

對于每個

Web應用程式

,Web容器都會建立一個

ServletContext

對象。 ServletContext對象用于從

部署描述符

(web.xml)擷取配置資訊,該資訊對于Web應用程式中的任何Servlet或JSP都可用。

ServletContext的一些重要方法 (Some Important method of ServletContext)

Methods Description
Object

getAttribute(String name)

returns the container attribute with the given name, or NULL if there is no attribute by that name.
String

getInitParameter(String name)

returns parameter value for the specified parameter name, or NULL if the parameter does not exist
Enumeration

getInitParameterNames()

returns the names of the context's initialization parameters as an Enumeration of String objects
void

setAttribute(String name,Object obj)

set an object with the given attribute name in the application scope
void

removeAttribute(String name)

removes the attribute with the specified name from the application context
方法 描述
對象

getAttribute(String name)

傳回具有給定名稱的容器屬性,如果該名稱沒有屬性,則傳回NULL。
字元串

getInitParameter(String name)

傳回指定參數名稱的參數值;如果參數不存在,則傳回NULL
枚舉

getInitParameterNames()

以字元串對象的枚舉形式傳回上下文的初始化參數的名稱

setAttribute(String name,Object obj)

無效
在應用程式範圍内設定具有給定屬性名稱的對象
void

removeAttribute(String name)

從應用程式上下文中删除具有指定名稱的屬性

如何在web.xml中初始化上下文參數 (How Context Parameter is Initialized inside web.xml)

Servlet中的ServletContext接口

如何擷取ServletContext的對象 (How to get the Object of ServletContext)

ServletContext app = getServletContext();OR
ServletContext app = getServletConfig().getServletContext();
           

ServletContext的優點 (Advantages of ServletContext)

  • Provides communication between servlets

    提供Servlet之間的通信

  • Available to all servlets and JSPs that are part of the web app

    适用于Web應用程式中的所有Servlet和JSP

  • Used to get configuration information from web.xml

    用于從web.xml擷取配置資訊

上下文初始化參數和Servlet初始化參數之間的差別 (Difference between Context Init Parameters and Servlet Init Parameter)

Context Init parameters Servlet Init parameter
Available to all servlets and JSPs that are part of web Available to only servlet for which the <init-param> was configured
Context Init parameters are initialized within the

<web-app>

not within a specific

<servlet>

elements
Initialized within the

<servlet>

for each specific servlet.
ServletContext object is used to get Context Init parameters ServletConfig object is used to get Servlet Init parameters
Only one ServletContext object for entire web app Each servlet has its own ServletConfig object
上下文初始化參數 Servlet初始化參數
适用于Web的所有Servlet和JSP 僅對配置了<init-param>的servlet可用
上下文初始化參數在

<web-app>

而不是在特定的

<servlet>

元素中初始化

<servlet>

為每個特定的servlet初始化。
ServletContext對象用于擷取Context Init參數 ServletConfig對象用于擷取Servlet的Init參數
整個Web應用程式隻有一個ServletContext對象 每個Servlet都有自己的ServletConfig對象

示範ServletContext用法的示例 (Example demonstrating usage of ServletContext)

web.xml web.xml
<web-app ...>

  <context-param>
    <param-name>driverName</param-name>
    <param-value>sun.jdbc.JdbcOdbcDriver</param-value>
  </context-param>
   
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>MyServlet</servlet-class>
  </servlet>
   
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping> 
     
</web-app>
           
MyServlet class : MyServlet類:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ServletContext sc = getServletContext();
        out.println(sc.getInitParameter("driverName"));   
    }
}
           
翻譯自: https://www.studytonight.com/servlet/servlet-context.php