zoukankan      html  css  js  c++  java
  • 13_ServletContext对象

    【简介】

    ServletContext即Servlet上下文对象,该对象表示当前的web应用环境信息,一个Web应用只会创建一个ServletContext对象。

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

    [注意]

    由于一个Web应用中的所有Servlet共享一个ServletContext对象,所以多个Servlet通过ServletContext对象实现数据共享,

    ServletContext对象通常称为Context对象。

    【ServletContext创建时机】

    ServletContext对象是在TomCat服务器加载完当前Web应用后创建出来的。

    ServletContext对象是作为ServletConfig对象成员变量传入Servlet中。

    通过ServletConfiggetServletContext()方法就可以得到ServletContext对象。

    看下ServletConfig中相关的ServletContext代码:

    class ServletConfig{      //ServletConfig对象中维护了ServletContext对象的应用
        ServletContext context;
        getInitParameter();
        getInitParameterNames();
        public ServletContext getServletContext(){     //返回一个ServletContext对象
          return contex;
        }
    }              

    在Servet中的init的方法实例化一个ServletConfig

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    [ 注意 ]

    this.ServletConfig.getServletContext():通过ServletConfig对象来获取ServletContext对象。

    ServletContext对象:启动时创建

    ServletConfig对象:调用init方法之前创建的,在ServletContext对象之前。

    【在Servlet中得到ServletContext的两种方式】

     

    【ServletContext的5大作用】

    1.获取web的上下文路径

      String getContextPath();

    2.获取全局的参数

      String getInitParameter(String name);

      Enumeration getInitParameterNames();

    3.和域对象相关的

      void setAttribute(String name,Onject object);

      Object getAttribute(String name);

      void removeAttribute(String name);

      域对象(域对象就是在不同资源之前来共享数据,保存数据,获取数据)

      ServletContext是我们学习的第一个域对象(Servlet共有三个域对象ServletContext、HttpServletRequest、HttpSession)

    4. 请求转发的

      RequestDispatcher getRequestDispatcher(String path);

      在Servlet跳转页面:

      4.1请求重定向(你找我借钱,我没有,你自己去找他借钱)

        1.地址栏会改变,变成重定向到的地址

        2.可以跳转到项目内的资源,也可以跳转项目外的资源

        3.浏览器向服务器发出两次请求,那么不能使用请求来作为域对象来共享数据。

      4.2请求转发(你找我借钱,我没有,我帮你去向他借钱)

        1.地址栏不会改变

        2.只能跳转到项目内的资源,不能跳转项目外的资源。

        3.浏览器向服务器发出一次请求,那么可以使用请求作为域对象共享数据。

    5.读取web项目的资源文件

      String getRealPath(String path);

      InputStream getResourceAsStream(String path);

      URL getResource(String path);

    【利用ServletContext对象来收发数据(Servlet3.0新特性)】

    利用ServletContext实现SendServlet和ReceiveServlet之间的数据共享

    【SendServlet.java 发数据】

    package com.Higgin.context;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/SendServlet")
    public class SendServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String data="Higgin";
            this.getServletContext().setAttribute("data", data);
            System.out.println("SendServlet发送的数据为:"+data);
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        }
    
    }

    【ReceiveServlet.java 收数据】

    package com.Higgin.context;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/ReceiveServlet")
    public class ReceiveServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String data=(String) this.getServletContext().getAttribute("data");
            System.out.println("ReceiveServlet接收到的数据:"+data);
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }

    [ 发送和接收数据(使用不同的浏览器,应用场景:聊天室) ]

    【通过ServletContext读取资源文件db.properties】

    【工程截图】

    [ db.properties ]

    url=jdbc:mysql://localhost:3306/test
    username=root
    password=123456

    [ ServletDemo00.java ]

    package com.Higgin.servlet;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/ServletDemo00")
    public class ServletDemo00 extends HttpServlet {
           
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
            
            InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
            Properties props=new Properties();  
            props.load(in);
    
            String url=props.getProperty("url");
            String username=props.getProperty("username");
            String password=props.getProperty("password");
            
            System.out.println("url=="+url);
            System.out.println("username=="+username);
            System.out.println("password=="+password);
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
        }
    
    }

    [ 运行结果 ]

  • 相关阅读:
    linux下文件的复制、移动与删除
    Hbase万亿级存储性能优化总结-配置
    hbase 读写和优化
    hive数据倾斜定位及处理
    flink初识及安装flink standalone集群
    【Linux】用less查看日志文件
    sqoop的详细使用及原理
    HBase删除数据的原理
    hbase数据加盐(Salting)存储与协处理器查询数据的方法
    Hbase内存磁盘大致关系
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5970186.html
Copyright © 2011-2022 走看看