4--Servlet之ServletContext

ServletContext是什么?
1、ServletContext 是一个接口,它表示 Servlet 上下文对象
2、一个 web 工程,只有一个 ServletContext 对象实例。
3、ServletContext 对象是一个域对象。
4、ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候销毁
如果学习过JSP的同学可能知道JSP中的九大内置对象,其实application就是该接口的实例。
ServletContext 类的主要作用
1、获取 web.xml 中配置的上下文参数 context-param
2、获取当前的工程路径,格式:/工程路径
3、获取工程部署后在服务器硬盘上的绝对路径
4、向application范围中存放数据

实例:

package com.qwy.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author qwy
 * @create 2021-03-09 21:21
 **/
public class ServletContextTest extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //通过ServletConfig对象获取ServletContext
        ServletContext context=   this.getServletConfig().getServletContext();
        //1.获取 <context-param>配置的参数
        String dirver = context.getInitParameter("dirver");
        System.out.println("dirver = " + dirver);
        String url = context.getInitParameter("url");
        System.out.println("url = " + url);
        //2.获 取 当 前 的 工 程 路 径 , 格 式 : / 工 程 路 径
        String contextPath = context.getContextPath();
        System.out.println("contextPath = " + contextPath);
        //3.获取虚拟目录对应的绝对路径
        String realPath = context.getRealPath("/");
        System.out.println("realPath = " + realPath);
    }

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

实例:web.xml

<!--配置全局参数-->
    <context-param>
        <param-name>dirver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/test</param-value>
    </context-param>
    <servlet>
        <servlet-name>ServletContextTest</servlet-name>
        <servlet-class>com.qwy.servlet.ServletContextTest</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletContextTest</servlet-name>
        <url-pattern>/context</url-pattern>
    </servlet-mapping>

浏览器地址栏访问:http://localhost:8080/servlet01/context
控制台输出:
dirver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/test
contextPath = /servlet01
realPath = D:\workspace\idea\servlet01\out\artifacts\servlet01_war_exploded(根据自己部署决定)

上例中我们使用ServletContext context= this.getServletConfig().getServletContext();获取的ServletContext对象
其实我们可以在doGet() 或doPost()方法中直接获取:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //获取ServletContext对象
        ServletContext servletContext = getServletContext();
    }

关于范围(作用域)的方法:

  1. setAttribute()
  2. getAtrribute()
  3. removeAttribute()
    见内置对象的四个范围

====================================================================================================
  自己也是最近才开始写文章,看到大家都分享自己的经验给别人。我也是受益者之一,做人不能只懂的索取而不去回馈,所以也将自己学习的知识分享给大家。我不能保证每个问题都讲的是对的,毕竟我也是个学习者。如果有地方写得不对或理解有误的,也希望大家给指出,我当非常感谢。编写过程也不易,也希望大家给个赞,关注下本人。这也能激励我继续分享。
  ====================================================================================================

猜你喜欢

转载自blog.csdn.net/qwy715229258163/article/details/114601685