zoukankan      html  css  js  c++  java
  • ServletContext详解 以及用法

    ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。

    换一种方式说吧,运行在Java虚拟机中的每一个Web应用程序都有一个与之相关的Servlet上下文。ServletContext对象是Web服务器中的一个已知路径的根,Servlet上下文被定位于http://localhost:8080/项目名.以 /项目名 请求路径(称为上下文路径)开始的所有请求被发送到与此ServletContext关联的Web应用程序。一个ServletContext对象表示了一个Web应用程序的上下文。

    Servlet上下文:Servlet上下文提供对应用程序中所有Servlet所共有的各种资源和功能的访问。Servlet上下文API用于设置应用程序中所有Servlet共有的信息。Servlet可能需要共享他们之间的共有信息。运行于同一服务器的Servlet有时会共享资源,如JSP页面、文件和其他Servlet。

    举例:

    如,做一个购物类的网站,要从数据库中提取物品信息,如果用session保存这些物品信息,每个用户都访问一便数据库,效率就太低了;所以要用来Servlet上下文来保存,在服务器开始时,就访问数据库,将物品信息存入Servlet上下文中,这样,每个用户只用从上下文中读入物品信息就行了。

    1.多个Servlet通过ServletContext对象实现数据共享。

    InitServletService方法中利用ServletContext对象存入需要共享的数据

    /*获取ServletContext对象*/  

    ServletContext context = this.getServletContext();   

    //存入共享的数据    

    context.setAttribute("name", "haha"); 

    在其它的Servlet中利用ServletContext对象获取共享的数据   

    /*获取ServletContext对象*/  

    ServletContext context = this.getServletContext();   

    //获取共享的数据   

    String name = context.getAttribute("name");   
    
    System.out.println("共享的内容值是:"+name);  

    2.获取WEB应用的初始化参数。

    web.xml文件中配置需要初始化的参数信息。

    <web-app>   
    
     <context-param>   
    
    <param-name>url</param-name>   
    
    <param-value>jdbc:MySQL://localhost:3306/4g</param-value>   
    
     </context-param>   
    
    <context-param>   
    
     <param-name>password</param-name>   
    
     <param-value>1314qr</param-value>   
    
     </context-param>   
    
     <context-param>   
    
      <param-name>user</param-name>   
    
      <param-value>root</param-value>   
    
      </context-param>   
    
    </web-app>  

    DemoServletdoPost方法中测试获取初始化参数的步骤如下:   

    /*获取ServletContext对象*/  

     ServletContext context = this.getServletContext();   

    /*获取初始化参数*/  

    //获取指定名称的初始化参数   

    String url = context.getInitParameter("url"); 

     //获取web.xml文件中所有的初始化应用参数          

     Enumeration<String> enumer = context.getInitParameterNames();   
    
    while(enumer.hasMoreElements()){   
    
    String name = enumer.nextElement();   
    
     String value = context.getInitParameter(name);   
    
     System.out.println(name+"=========="+value);   
    
        }   

    2.实现Servlet的转发:

    在测试的Servlet中实现转发的步骤如下:  

    /*要利用ServletContext对象实现转发获取对象*/  

    ServletContext context = this.getServletContext();   

     //request对象中存入name属性    

    request.setAttribute("name", "haha");   

     /*根据转发的地址获取 RequestDispatcher对象*/  

    RequestDispatcher  rd  = context.getRequestDispatcher("/index.jsp");   

    //调用转发方法 以下采用任意方法即可    

    rd.forward(request, response);   

      //rd.include(request, response);   

    注意:forwardinclude的区别 

    forward方法是把请求的内容转发到另外的一个servlet.include是把另一个servlet处理过后的内容拿过来.

    (forward方法调用后在响应中的没有提交的内容被自动消除。将请求转发给其他的Servlet后,由

        被调用的Servlet负责对请求做出响应,而原先Servlet的执行则终止。      

       include方法使原先的Servlet和转发到的Servlet都可以输出响应信息,即原先的Servlet还可以继续输出响应信息)

    3.利用ServletContext对象读取资源文件。  

    读取资源文件(properties文件(属性文件))的三种方式

    配置的properties的内容如下:   

    url=jdbc:mysql://localhost:3306/3g ; 

    user=root;

    password=root;  

    获取实现的代码如下:   

    /*获取ServletContext对象*/  

    ServletContext context = this.getServletContext();     

    //第一种方式    

    URL url = context.getResource("WEB-INF/classes/db.properties");   

    InputStream is =  url.openStream();   

    //第二种方式   

     /*读取db.properties文件*/  

    String path =context.getRealPath("WEB-INF/classes/db.properties");   

     /*根据文件的路径 构建文件对象*/  

    File file = new File(path);   

     /*根据file文件对象 创建输入流*/  

    InputStream is = new FileInputStream(file);   

    //第三种方式   

    InputStream is = context.getResourceAsStream("WEB-INF/classes/db.properties ");    

     以三种方式任意一种可以:    

      /*解析properties的文件*/  

         Properties prop = new Properties();   

         //从输入流中读取属性列表(键和元素对)。   

          prop.load(is);   

          Set<String> set = prop.stringPropertyNames();   

           //遍历set集合   

           Iterator<String> it = set.iterator();   

           while(it.hasNext()){   

               String key = it.next();   

               String value = prop.getProperty(key);   

               System.out.println(key+"-----"+value);   

                  }  

    作者:逆舟
    https://www.cnblogs.com/zy-jiayou/
    本博客文章均为作者原创,转载请注明作者和原文链接。
  • 相关阅读:
    DPDK安装方法 17.12.13
    numa.h:No such file or directory 解决方法
    17秋 软件工程 第六次作业 Beta冲刺 Scrum3
    17秋 软件工程 第六次作业 Beta冲刺 总结博客
    17秋 软件工程 第六次作业 Beta冲刺 Scrum2
    Paper Reviews and Presentations
    17秋 软件工程 第六次作业 Beta冲刺 Scrum1
    17秋 软件工程 第六次作业 Beta冲刺
    error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted
    17秋 软件工程 个人作业 软件产品案例分析
  • 原文地址:https://www.cnblogs.com/zy-jiayou/p/7284402.html
Copyright © 2011-2022 走看看