ServletContext接口简介

ServletContext接口简介


-- ServletContext接口简介

WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,ServletContext对象包含Web应用中所有 Servlet 在 Web 容器中的一些数据信息。ServletContext随着Web应用的启动而创建,随着 Web 应用的关闭而销毁。一个 Web 应用只有一个ServletContext 对象。
ServletContext中不仅包含了 web.xml 文件中的配置信息,还包含了当前应用中所有Servlet可以共享的数据。可以这么说, ServeltContext 可以代表整个应用,所以ServletContext有另外一个名称:application。



-- ServletContext中常用方法

ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext()方法获得ServletContext对象。
  • String getInitParameter ():获取 web.xml 文 件 的 中 指 定 名 称 的上下文参数值 。
  • Enumeration getInitParameterNames():获取 web.xml 文件的中的所有的上下文参数名称。其返回值为枚举类型 Enumeration。
  • void setAttribute(String name, Object object):在 ServletContext 的公共数据空间中,也称为域属性空间,放入数据。这些数据对于 Web应用来说,是全局性的,与整个应用的生命周期相同。当然,放入其中的数据是有名称的,通过名称来访问该数据。
  • Object getAttribute(String name):从 ServletContext 的域属性空间中获取指定名称的数据。
  • void removeAttribute(String name):从 ServletContext 的域属性空间中删除指定名称的数据。
  • String getRealPath(String path):获取当前 Web 应用中指定文件或目录在本地文件系统中的路径。
  • String getContextPath():获取当前应用在 Web 容器中的名称。
<!-- 初始化参数 -->
  <context-param>
      <param-name>MySQLDriver</param-name>
      <param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
  <context-param>
      <param-name>dbURL</param-name>
      <param-value>jdbc:mysql:</param-value>
  </context-param>

创建servlet:
public class ContextTest01 implements Servlet {

private ServletConfig config;

    @Override
    public void destroy() {

    }

    @Override
    public ServletConfig getServletConfig() {
        return this.config;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.config = servletConfig;
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        ServletContext application = this.config.getServletContext();
        System.out.println("ContextTest01:" + application);

        String driver = application.getInitParameter("MySQLDriver");
        System.out.println(driver);

        String contextPath = application.getContextPath();
        System.out.println("contextPath:" + contextPath);

        //文件在硬盘中的绝对路径
        String realPath = application.getRealPath("FirstServlet");
        System.out.println("realPath:" + realPath);

        //向ServletContext中添加属性
        application.setAttribute("admin", "tiger");
        application.setAttribute("password", 123456);
        //删除password
        application.removeAttribute("password");
    }

}

再创建一个servlet:取出上一个Servlet中放入到ServletConetxt中的数据, (结论:可以把一个项目的多个Servlet要用的数据放入到Servletcontext中)

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ContextTest02 implements Servlet {

    private ServletConfig config;

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

    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        ServletContext application = this.config.getServletContext();
        System.out.println("Context02中的application:" + application);


        String admin = (String)application.getAttribute("admin");
        System.out.println(admin);
        String password = (String)application.getAttribute("password");
        System.out.println(password);
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}







发布了15 篇原创文章 · 获赞 39 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37024565/article/details/80682785
今日推荐