• 【JavaWeb】ServletContext配置信息


    为什么使用ServletContext

    ServletContext的对象由Web容器在部署项目时创建。 该对象可用于从web.xml文件获取配置信息。 每个Web应用程序只有一个ServletContext对象。
    如果有信息要共享给多个servlet使用,最好在web.xml文件中使用元素提供它。

    ServletContext的优点

    如果有任何信息要共享给所有的servlet使用,并且要让它容易维护,最好的办法就是在web.xml文件中提供这些信息,所以如果信息要更改直接在web.xml中修改,而不需要修改servlet代码。

    ServletContext接口的使用

    有很多ServletContext对象可以使用。 其中一些如下:

    • ServletContext对象提供容器和servlet之间的接口。
    • 使用ServletContext对象在web.xml文件获取配置信息。
    • ServletContext对象可用于设置,获取或删除web.xml文件中属性。
    • ServletContext对象可用于提供应用程序间通信。

    常用的ServletContext接口方法

    给出了一些常用的ServletContext接口方法。

    序号方法描述
    1public String getInitParameter(String name)返回指定参数名称的参数值。
    2public Enumeration getInitParameterNames()返回上下文的初始化参数的名称。
    3public void setAttribute(String name,Object object)在应用程序范围内设置给定的对象。
    4public Object getAttribute(String name)返回指定名称的属性。
    5public Enumeration getInitParameterNames()返回上下文的初始化参数的名称,作为String对象的枚举。
    6public void removeAttribute(String name)从servlet上下文中删除给定名称的属性。

    如何获取ServletContext接口的对象?

    • 通过ServletConfig接口的getServletContext()方法返回ServletContext对象。
    • 通过GenericServlet类的getServletContext()方法返回ServletContext对象。

    getServletContext()方法的语法

    public ServletContext getServletContext()
    
    • 1

    getServletContext()方法的示例

    ServletContext application=getServletConfig().getServletContext();  
     
    ServletContext application=getServletContext();
    
    • 1
    • 2
    • 3

    Context范围内提供初始化参数的语法

    Web应用程序的context-param元素的子元素用于定义应用程序范围中的初始化参数。 参数名称和参数值是context-param的子元素。param-name元素定义参数名称,param-value定义其值。参考以下配置代码片段 -

    <web-app>  
     ......  
      <context-param>  
        <param-name>parameter_nameparam-name>  
        <param-value>parameter_valueparam-value>  
      context-param>
     ......  
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    获取初始化参数的ServletContext示例

    ContextServlet.java

    public class ContextServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            // 设置服务器内容类型
            response.setContentType("text/html");
            // 获取服务器的输出对象
            PrintWriter out = response.getWriter();
    
            // 获取ServletContext对象
            ServletContext context = getServletContext();
    
            String driverName = context.getInitParameter("dname");
    
            out.println("driver name is "+driverName+"");
            out.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Context.html

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ContextServlettitle>
    head>
    <body>
    <div style="text-align: center"><a href="context">点击这里a>查看Context信息
    div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    web.xml

    
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id="WebApp_ID" version="3.1">
        <display-name>ContextServletdisplay-name>
        <welcome-file-list>
            <welcome-file>Context.htmlwelcome-file>
        welcome-file-list>
        <servlet>
            <servlet-name>ContextServletservlet-name>
            <servlet-class>ContextServletservlet-class>
        servlet>
        <context-param>
            <param-name>dnameparam-name>
            <param-value>com.mysql.jdbc.Driverparam-value>
        context-param>
        <servlet-mapping>
            <servlet-name>ContextServletservlet-name>
            <url-pattern>/contexturl-pattern>
        servlet-mapping>
    we-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    Seata分布式事务模型和基本应用
    Vscode 快速下载
    关于POM声明为provided的依赖,运行程序时报错NoClassDefFoundError
    Java中的stream流[75]
    Spark通过三种方式创建DataFrame
    精益(Lean)与ERP实施
    Unity接入腾讯云
    怎样做好影视作品字幕翻译?
    Halcon Solution Guide I basics(2): Image Acquisition(图像加载)
    protobuf全局环境搭建
  • 原文地址:https://blog.csdn.net/u013301892/article/details/127562141