zoukankan      html  css  js  c++  java
  • [Servlet]研究ServletContext对象

    作者信息
    作者姓名:金云龙
    个人站点:http://www.longestory.com
    个人公众帐号:搜索“longestory”或“龙哥有话说

    ServletContext概述

    ServletContext对象是Servlet三大域对象之中的一个,每一个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者上下文。Tomcatserver在启动时,会自己主动创建一个ServletContext对象,在关闭时,会自己主动销毁这个ServletContext对象。每一个Web应用程序仅仅拥有一个ServletContext对象,ServletContext对象能够在整个Web应用中共享数据资源。

    下列是ServletContext提供的方法列表:

    Method Summary
    Object getAttribute(String name)
    Enumeration getAttributeNames()
    String getInitParameter(String name)
    Enumeration getInitParameterNames()
    String getMimeType(String file)
    String getRealPath(String path)
    String getServletContextName()
    Enumeration getServletNames()
    void log(String msg)
    void removeAttribute(String name)
    void setAttribute(String name, Object object)

    获取ServletContext对象

    在自己定义Servlet中有以下几种方式获取到ServletContext对象:

    • 通过ServletConfig对象的getServletContext()方法获取。

    • 通过继承GenericServlet类或HttpServlet类,调用GenericServlet类或HttpServlet类的getServletContext()方法获取。

    我们通过一个案例来讨论一下。

    • 首先,创建一个自己定义Servlet。用来获取ServletContext对象。

    public class AServlet extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            ServletConfig config = getServletConfig();
            ServletContext context1 = config.getServletContext();
            context1.log("这是通过ServletConfig对象获取到的ServletContext对象.");
            ServletContext context2 = getServletContext();
            context2.log("这是通过继承GenericServlet类获取到的ServletContext对象.");
        }
    }
    
    • 在web.xml文件里配置Servlet相关信息。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <servlet>
        <servlet-name>AServlet</servlet-name>
        <servlet-class>app.java.context.AServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>AServlet</servlet-name>
        <url-pattern>/servlet/AServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    

    这里写图片描写叙述

    • 通过ServletContext对象的log(Stirng msg)方法,能够向控制台信息打印。

    配置全局初始化參数

    在web.xml文件里,使用定义的初始化參数。仅仅能在当前Servlet中使用,而其它Servlet是无权限訪问当前Servlet下配置的初始化參数的。而能够使用ServletContext在web.xml文件里配置全局初始化參数,这样当前Web应用程序中的全部Servlet都能够訪问。

    • 在web.xml文件里使用<context-param>来定义全局初始化參数。
    <?

    xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>weixin</param-name> <param-value>longestory</param-value> </context-param> <servlet> <servlet-name>AServlet</servlet-name> <servlet-class>app.java.context.AServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AServlet</servlet-name> <url-pattern>/servlet/AServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>BServlet</servlet-name> <servlet-class>app.java.context.BServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BServlet</servlet-name> <url-pattern>/servlet/BServlet</url-pattern> </servlet-mapping> </web-app>

    • 在两个自己定义Servlet中。分别利用ServletContext对象获取全局初始化參数。
    public class BServlet extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            ServletContext context = getServletContext();
    
            String weixin = context.getInitParameter("weixin");
            System.out.println(weixin);
        }
    }
    

    在自己定义Servlet中,能够通过ServletContext对象的getInitParameter(String name)方法获取相应參数名称的全局初始化參数值。也能够通过ServletContext对象的getInitParameterNames()方法获取全部全局初始化參数的名称。

    还能够通过ServletContext对象的getMineType(String file)方法依据文件扩展名获取文件MIME类型。

    public class BServlet extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            ServletContext context = getServletContext();
    
            String html = context.getMimeType("1.html");
            String css = context.getMimeType("2.css");
            String javascript = context.getMimeType("3.js");
            System.out.println("HTML的文件类型为"+html+", CSS的文件类型为"+css+", javascript的文件类型为"+javascript);
        }
    }
    

    公布Web应用程序,并启动Tomcatserver,在控制台中打印:

    HTML的扩展名为text/html, CSS的扩展名为text/css, javascript的扩展名为application/javascript

    ServletContext对象的getMineType(String file)方法会自己主动读取Tomcat安装文件夹中conf文件夹中的web.xml文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <mime-mapping>
            <extension>html</extension>
            <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>css</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping>
        <mime-mapping>
            <extension>js</extension>
            <mime-type>application/javascript</mime-type>
    </mime-mapping>
    </web-app>
    

    多个Servlet共享数据

    在同一个Web应用程序中,多个Servlet之间能够共享ServletContext对象中的数据信息。

    主要是通过ServletContext对象的setAttribute(String name, Object object)方法和getAttribute(String name)方法完毕,以下我们来实现统计站点訪问次数的案例。

    • 创建一个VisitServlet用来获取訪问次数。并存储在ServletContext对象中。
    public class VisitServlet extends HttpServlet {
        @Override
        public void init() throws ServletException {
            ServletContext context = getServletContext();
            context.setAttribute("times", 0);
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = getServletContext();
    
            int times = (Integer)context.getAttribute("times");
            times ++;
            context.setAttribute("times", times);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    
    • 创建一个ShowTimeServlet用来显示訪问次数。
    public class ShowTimeServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = getServletContext();
    
            int times = (Integer)context.getAttribute("times");
    
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<h1>VisitServlet共被訪问了"+times+"次</h1>");
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    
    • 配置web.xml文件里有关Servlet信息。
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <servlet>
        <servlet-name>VisitServlet</servlet-name>
        <servlet-class>app.java.context.VisitServlet</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>ShowTimeServlet</servlet-name>
        <servlet-class>app.java.context.ShowTimeServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>VisitServlet</servlet-name>
        <url-pattern>/visit</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>ShowTimeServlet</servlet-name>
        <url-pattern>/show</url-pattern>
      </servlet-mapping>
    </web-app>
    

    这里写图片描写叙述

    读取Webproject中资源文件

    读取project中的资源文件,Java中的IO流事实上就能够完毕,以下使用Java中的IO流完毕读取资源文件。

    • 首先在Webproject中,创建四个资源文件。
      • 在Webproject的根文件夹下创建1.txt。

      • 在Webproject的WebRoot文件夹下创建2.txt。
      • 在Webproject的WebRoot文件夹的WEB-INF文件夹下创建3.txt。
      • 在Webproject的src文件夹下创建4.txt。

    • 创建一个Java文件用于读取上述的四个资源文件。
    public class ReaderFileTest {
        // 编写readfile()方法完毕资源文件的读取工作.
        public static void readfile(String fileName) throws Exception{
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        }
    
        public static void main(String[] args) throws Exception {
            // 读取1.txt
            String filename1 = "1.txt";
            readfile(filename1);
            // 读取2.txt
            String filename2 = "WebRoot/2.txt";
            readfile(filename2);
            // 读取3.txt
            String filename3 = "WebRoot/WEB-INF/3.txt";
            readfile(filename3);
            // 读取4.txt
            String filename4 = "src/4.txt";
            readfile(filename4);
        }
    }
    
    • 执行该Java文件会在控制台打印响应信息。

    这里写图片描写叙述

    假设要想利用Servlet API的内容来读取Webproject中的资源文件,又要怎样来做呢?ServletContext对象的getRealPath()方法能够来完毕此项工作。

    • 创建一个自己定义Servlet,使用ServletContext对象的getRealPath()方法来完毕读取资源文件。

    public class ReadFileServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            OutputStream out = response.getOutputStream();
            /*
             *  读取1.txt
             *   * 由于1.txt资源文件在Webproject的根文件夹.
             *   * Webproject的WebRoot文件夹公布到Tomcatserver.
             *   * 所以,1.txt资源文件是不会公布到Tomcatserver的,Servlet无法读取.
             */
            // 读取2.txt
            String filename2 = getServletContext().getRealPath("/2.txt");
            InputStream in2 = new FileInputStream(new File(filename2));
            IOUtils.copy(in2, out);
            // 读取3.txt
            String filename3 = getServletContext().getRealPath("/WEB-INF/3.txt");
            InputStream in3 = new FileInputStream(new File(filename3));
            IOUtils.copy(in3, out);
            // 读取4.txt
            String filename4 = getServletContext().getRealPath("/WEB-INF/classes/4.txt");
            InputStream in4 = new FileInputStream(new File(filename4));
            IOUtils.copy(in4, out);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    

    这里写图片描写叙述

    除了能够使用ServletContext对象的getRealPath()方法之外,还能够使用ServletContext对象的getResourceAsStream()方法来完毕读取资源文件的工作。

    public class ReadFileServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
            InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/4.txt");
            IOUtils.copy(in, out);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    

    另一种通用的方法:利用Class类的getResource()方法也能够完毕读取资源文件的工作。

    public class ReadFileServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 利用类载入器读取Webproject的资源文件
            String filename = ReadFileServlet.class.getResource("/4.txt").getFile();
            InputStream in = new FileInputStream(new File(filename));
            IOUtils.copy(in, out);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    

    转载说明:请注明作者及原文链接。谢谢!

  • 相关阅读:
    [LeetCode] Rotate Image
    [LeetCode] Generate Parentheses
    pandas 使用总结
    ConfigParser 读写配置文件
    Cheat Sheet pyspark RDD(PySpark 速查表)
    python随机生成字符
    grep 命令
    hadoop 日常使用记录
    python 2 计算字符串 余弦相似度
    screen命令
  • 原文地址:https://www.cnblogs.com/llguanli/p/8400290.html
Copyright © 2011-2022 走看看