day15-Servlet04( 二 )


day15-Servlet04

文章插图
问题二:这是为什么呢?
我们先来梳理ServletConfigconfig的使用流程
  1. 当DBServlet对象初始化时 , Tomcat会同时创建一个ServletConfig对象
  2. 如果DBServlet init()方法中调用了super.init(config);
  3. 就会调用父类GenericServlet的init方法:
    public void init(ServletConfig config) throws ServletException { this.config = config; this.init();}这时就会把Tomcat创建的ServletConfig对象赋给GenericServlet的属性config
  4. 因此如果要重写init()方法 , 记住如果你想在其他方法通过getServletConfig()获取ServletConfig,则一定要记住调用super.init(config);
回到问题二:
如果没有把tomcat创建的ServletConfig,赋值给GenericServlet的属性config 。那么GenericServlet的属性config的值就为null,而doPost或者doGet方法通过getServletConfig()拿到的就是GenericServlet的属性config,因此就会输出null 。
侧面证实了方法中获取的servletConfig是同一个对象(问题一)
因此上面的例子中,浏览器访问DBServlet,发现出现了500错误的原因是,doPost方法中获取了为null的ServletConfig对象中的属性
day15-Servlet04

文章插图
13.ServletContext13.1为什么需要ServletContext
先来看一个需求:如果我们希望统计某个web应用的所有Servlet被访问的次数,怎么办?
方案一:使用DB
day15-Servlet04

文章插图
方案二:使用ServletContext
day15-Servlet04

文章插图
13.2ServletContext基本介绍
  1. ServletContext是一个接口,它表示Servlet上下文对象
  2. 一个web工程中,只有一个ServletContext对象实例
  3. ServletContext对象是在web工程启动的时候创建的 , 在web工程停止的时候销毁
  4. 可以通过ServletConfig.getServletContext方法获得对ServletContext对象的应用,也可以通过this.getServletContext()来获得其对象的引用
  5. 由于一个web应用中的所有Servlet共享一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现多个Servlet间的通信 。ServletContext对象通常也被称为域对象 。
    day15-Servlet04

    文章插图
13.3ServletContext可以做什么
  1. 获取web.xml文件中配置的上下文参数context-param [信息和整个web应用相关,而不是属于某个Servlet]
  2. 获取当前的工程路径,格式:/工程路径
  3. 获取工程部署后在服务器硬盘上的绝对路径
    比如D:\IDEA-workspace\servlet\out\artifacts\servlet_war_exploded
  4. 向Map一样存取数据,多个Servlet共享数据
13.4应用实例13.4.1应用实例1-获取工程相关信息需求如下:
  1. 获取web.xml中配置的上下文参数context-param
  2. 获取当前的工程路径,格式:/工程路径
  3. 获取工程部署后在服务器硬盘上的绝对路径
配置ServletContext_: 在web.xml文件增加相关配置
<!--配置ServletContext_--><servlet><servlet-name>ServletContext_</servlet-name><servlet-class>com.li.servlet.servletcontext.ServletContext_</servlet-class></servlet><servlet-mapping><servlet-name>ServletContext_</servlet-name><url-pattern>/servletContext_</url-pattern></servlet-mapping><!--配置整个网站的信息--><context-param><param-name>website</param-name><param-value>http://www.lili.net</param-value></context-param><context-param><param-name>company</param-name><param-value>lili有限公司</param-value></context-param>ServletContext_:
package com.li.servlet.servletcontext;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;public class ServletContext_ extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取web.xml的context-parameter//1.获取到ServletContext对象ServletContext servletContext = getServletContext();//2.获取websiteString website = servletContext.getInitParameter("website");String company = servletContext.getInitParameter("company");System.out.println("website= " + website);System.out.println("company= " + company);//3.获取项目的工程路径String contextPath = servletContext.getContextPath();System.out.println("项目路径= " + contextPath);// /servlet_demo//4.得到项目发布后真正的工作路径//这里的斜杠/表示我们的项目发布后的根路径 D:\IDEA-workspace\servlet_demo\out\artifacts\servlet_demo_war_explodedString realPath = servletContext.getRealPath("/");System.out.println("项目发布后的绝对路径= " + realPath);}}

推荐阅读