ServletConfig:表示servlet的配置信息,一个servlet对象对应一个servletconfig对象   方法: 1.获取初始化参数 config.getInitParameter()     ServletContext:表示servlet的全局配置信息,一个WebApplication只有一个ServletContext对象,该对象被所有Servlet共用   方法: 1.获取全局的初始化参数 context.getInitParameter() 2.获取上下文路径(部署在tomcat中的项目目录名) context.getContextPath() 3.获取文件的绝对路径(从WebRoot下开始定位文件) context.getRealPath() 4.获取资源,将资源作为流返回 context.getResourceAsStream(); 5.显示目录下的资源 Set<String> paths = sc1.getResourcePaths(); 6.存储一个key-value数据 context.setAttribute(key,value) 根据key获取value context.getAttribute(key)   ServletConfig:
//web中
  <init-param>
        <param-name>listings</param-name>  
      <param-value>true</param-value>
  </init-param>
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.获取ServletConfig对象
                // 表示servlet的配置信息
                ServletConfig config = this.getServletConfig();
                //2.使用config读取初始化参数
                String value = config.getInitParameter("listings");
                System.out.println(value);
                //获取servletName
                System.out.println(config.getServletName());
        
    }

}
/*@Override
    public void init(ServletConfig config) throws ServletException {
        String value = config.getInitParameter("listings");
        System.out.println(value);
    }*/

ServletContext

//web中
<context-param>
         <param-name>aaa</param-name>
         <param-value>bbb</param-value>
     </context-param>
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //1.获取ServletContext:servlet的全局对象,整个项目就一个ServletContext对象
        ServletContext sc1 = this.getServletContext();
        ServletContext sc2 = request.getServletContext();
        ServletContext sc3 = this.getServletConfig().getServletContext();
        
        //2.使用该对象
        //读取全局配置信息
        String val = sc1.getInitParameter("aaa");
        System.out.println(val);
        
        //* 获取上下文路径
        String contextPath = sc1.getContextPath();
        System.out.println(contextPath);
        
        //* 获取文件的绝对路径:从WebRoot下开始定位文件
        String realPath = sc1.getRealPath("/image/1.jpg");
        System.out.println(realPath);
        
        //获取资源,将资源作为流返回
        InputStream in = sc1.getResourceAsStream("/WEB-INF/car.properties");
        
        //显示目录下的资源
        Set<String> paths = sc1.getResourcePaths("/aaa");
        for (String string : paths) {
            System.out.println(string);
        }
        
        
    }

 

作用域对象 生命周期 作用范围 HttpServletRequest 一次请求 一次请求经过的所有servlet HttpSession 一次会话 一次会话中,所有的servlet ServletContext 项目从加载到卸载 一个项目中,所有servlet     作用域对象,有三个方法: 1.setAttribute(String key,Object value); 2.getAttribute(String key); 3.removeAttribute(String key);
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.request对象 
        request.setAttribute("name", "zhangsan");
        //request.getRequestDispatcher("scope2").forward(request, response);
        
        //2.session对象 先向浏览器输入,在用下面输出
        HttpSession session = request.getSession();
        session.setAttribute("age", 18);
        
        //3.application对象  先向浏览器输入,所有浏览器都可以输出
        ServletContext sc = request.getServletContext();
        sc.setAttribute("gender", "男");
        
    }
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.获取request对象中数据
        /*String name = (String) request.getAttribute("name");
        System.out.println(name);
        
        //2.获取session对象中的数据
        int age = (int) request.getSession().getAttribute("age");
        System.out.println(age);*/
        
        //3.获取application对象中的数据
        String gender = (String) request.getServletContext().getAttribute("gender");
        System.out.println(gender);
        
    }

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。              
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄