ServletContext

ServletContext

1、简介

web容器在启动时会为每个web程序创建一个ServletContext对象,它代表了当前的web应用

2、应用(以下方法现在几乎不用)

  • 共享数据(setAttribute(),getAttribute())

    在一个Servlet中保存的数据,可以在另一个Servlet中拿到,例:

    放置类

    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String userName = "张晟伟";
            context.setAttribute("userName",userName);//将userName放进content对象
        }
    }
    

    获取类

    public class GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String userName = (String)context.getAttribute("userName");
            resp.setContentType("text/html");
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().println("名字:"+userName);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

     <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.zhang.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>getcontext</servlet-name>
            <servlet-class>com.zhang.servlet.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>getcontext</servlet-name>
            <url-pattern>/getc</url-pattern>
        </servlet-mapping>
    

    测试结果:

    ServletContext

    原理图:

    ServletContext

  • 获取初始化参数(getInitParameter())

    java代码

    public class ServletDemo_01 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String url = context.getInitParameter("url");
            resp.getWriter().println(url);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

     <!--
        context-param标签为配置一些web应用的初始化参数
        -->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>
        <servlet>
            <servlet-name>gp</servlet-name>
            <servlet-class>com.zhang.servlet.ServletDemo_01</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>gp</servlet-name>
            <url-pattern>/gp</url-pattern>
        </servlet-mapping>
    
  • 请求转发(RequestDispatcher)

    原理图:

    ServletContext

    java代码

    public class ServletDemo_04 extends HelloServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
    //        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//请求参数对象,转发请求路径
    //        requestDispatcher.forward(req,resp);//调用方法实现请求转发
            context.getRequestDispatcher("/gp").forward(req,resp);//将被注释的两行代码合并
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

    <servlet>
            <servlet-name>respdispatcher</servlet-name>
            <servlet-class>com.zhang.servlet.ServletDemo_04</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>respdispatcher</servlet-name>
            <url-pattern>/rc</url-pattern>
        </servlet-mapping>
    

    实现效果:

    !ServletContext

  • 读取资源文件(Properties)

    新建Properties

    • 在resources下新建properties文件

      ServletContext

      ServletContext

      ServletContext

      启动服务器propertis文件导出

      ServletContext

    • 在java目录下新建properties文件

      注:由于在maven中约定大于配置,所以写在java目录下的propertis文件可能无法被导出或者生效

      ServletContext

      启动服务器发现在target文件相应路径下并没有aa.properties文件

      ServletContext

      解决方法

      在pom.xml文件中配置resources

      <!--在build中配置resources,来防止我们资源导出失败问题-->
          <build>
              <resources>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>true</filtering>
                  </resource>
                  <resource>
                  <directory>src/main/java</directory>
                  <includes>
                      <include>**/*.properties</include>
                      <include>**/*.xml</include>
                  </includes>
                  <filtering>true</filtering>
              </resource>
              </resources>
          </build>
      

      重启服务器,问题解决

      ServletContext

    • 发现properties文件都被打包到了同一个路径下:classes,我们俗称 这个路径为 classpath(类路径)

    读取Properties

    需要一个流及properties中的内容

    ServletContext

    java代码

    public class ServletDemo_05 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            InputStream i = context.getResourceAsStream("/WEB-INF/classes/db.properties");
            Properties prop = new Properties();
            prop.load(i);
            String user = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            resp.getWriter().println(user+":"+pwd);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

    <servlet>
        <servlet-name>a_05</servlet-name>
        <servlet-class>com.zhang.servlet.ServletDemo_05</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>a_05</servlet-name>
        <url-pattern>/rp</url-pattern>
    </servlet-mapping>
    

    实现效果

    ServletContext

上一篇:Adobe Photoshop CC中文版本注册安装教程


下一篇:Servlet 程序的实现