servletContext对象

  1. 由Servlet容器负责创建,对于每个JavaWeb应用,在启动时,Servlet容器都会创建一个ServletContext对象。它代表当前web应用。
  2. ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。
  3. ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
    由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
  4. servletContext生命周期:
    服务器启动的时候,会为每一个web应用创建一个servetContext,服务器里面有多少个web应用机会有创建多少个servlet。
    当服务器停止运行的时候servletContext会销毁,或者web工程被删除的时候,会被销毁。
    1. 在一个web应用中,所有的servlet可以共享servlet里面的数据:
public class servletContext extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String value="aaa";
        //通过servletConfig获得servletContext,将数据存入servletContext
        this.getServletConfig().getServletContext().setAttribute("data", value);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

}

servletContext2 可以得到servletContext中的数据

public class servletContext2 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String value=(String)this.getServletContext().getAttribute("data");
        System.out.println(value);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
  1. /实际开发中可以在web.xml文件中配置,当web容器启动的时候,初始化参数会自动被封装到servletContext中。
<context-param>
    <param-name>data</param-name>
    <param-value>HELLO我是初始化参数</param-value>
  </context-param>
public class servletContext3 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String value=this.getServletContext().getInitParameter("data");
        System.out.println(value);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
  1. servletContex实现请求转发:当一个servlet无法单独完成客户端的请求时,这时就要用到其他的servlet对请求信息进行处理。
    使用方法:public void forward(ServletRequest request, ServletResponse response) throws ServletException,IOException;
public class servletContextDispatcher extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //请求转发
        String data="aaa";
        this.getServletContext().setAttribute("data", data);

        RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");
        rd.forward(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

1.jsp

<body>

    <font color="red">
        <%
        String data=(String)application.getAttribute("data");
        out.write(data);
        %>
    </font>
</body>
  1. servletContext读取资源文件:
    资源文件一般有两种:xml里面的数据有关,properties里面的数据无关:
    db.properties:
url=jdbc:mysql://localhost:3306/test
username=root
password=root
public class servletContext4 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //注意将工程发布到服务器上以后,没有src文件,为classes文件
        InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        //所有的properties文件都用properties处理
        Properties props=new Properties();
        props.load(in);

    System.out.println(props.getProperty("url")+","+props.getProperty("username")+","+props.getProperty("password"));
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletContext是Java Web中的一个重要对象,表示整个Web应用程序的上下文环境。它是一个接口,提供了许多方法,用于获取Web应用程序的相关信息,例如应用程序的名称、版本、servlet上下文参数、初始化参数等。 以下是ServletContext对象的一些常用方法和用法: 1. 获取应用程序的名称和版本: String appName = context.getServletContextName(); //获取应用程序名称 String appVersion = context.getMajorVersion() + "." + context.getMinorVersion(); //获取应用程序版本号 2. 获取servlet上下文参数: String paramValue = context.getInitParameter("paramName"); //获取指定参数的值 Enumeration<String> paramNames = context.getInitParameterNames(); //获取所有参数名称 3. 获取应用程序的真实路径: String realPath = context.getRealPath("/"); //获取应用程序的根目录真实路径 4. 获取应用程序的资源: InputStream input = context.getResourceAsStream("/path/to/resource"); //获取指定资源的输入流 URL resourceUrl = context.getResource("/path/to/resource"); //获取指定资源的URL 5. 获取应用程序的Servlet信息: ServletInfo info = context.getServletInfo(); //获取Servlet的信息 6. 获取应用程序的Session管理器: HttpSessionManager sessionMgr = context.getSessionManager(); //获取Session管理器 7. 获取应用程序的Mime类型: String mimeType = context.getMimeType("fileName"); //获取指定文件的Mime类型 8. 获取应用程序的Servlet上下文: ServletContext servletContext = context.getContext("/path/to/servlet"); //获取指定Servlet的上下文 总之,ServletContext对象提供了一种方便的方式来获取Web应用程序的各种信息和资源,使得开发人员可以更方便地开发和管理Web应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值