【JavaWeb】上下文对象

18 篇文章 0 订阅

上下文对象

1、什么是上下文对象


Web容器在启动以后,里面会有很多web的程序,容器会问每一个web程序创建一个队员的ServletConntext对象,这是一个全局变量,每个web程序都可以通过一些组件读取他,这样有助于数据的共享

在这里插入图片描述

上下文对象是 服务器容器的全局变量所有的servlet访问的上下文都是同一个,但是 当服务器关闭后,上下文对象消失

获取上下文对象:

// 获取本服务器的上下文对象
ServletContext sc = this.getServletContext();

2、上下文对象的常用方法


常用方法:

  • 绑定数据

    setAttribute(String key,Object value);
    
    @WebServlet("/first")
    public class FirstServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 获取servlet上下文对象
            ServletContext sc = this.getServletContext();
            // 绑定参数
            String str = "每个人的身上都有毛毛...";
            sc.setAttribute("song",str);
        }
    }
    
  • 获取绑定的数据:

    getAttribute(String key);
    
    @WebServlet("/second")
    public class SecondServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 所有的servlet都可以调用同一个上下文对象
            // 获取上下文对象
            ServletContext sc = this.getServletContext();
            // 获取绑定参数
            String song =(String)sc.getAttribute("song");
            System.out.println(song);
        }
    }
    
  • 移除(删除)数据:

    removeAttribute(String key);
    
    @WebServlet("/second")
    public class SecondServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 获取上下文对象
            ServletContext sc = this.getServletContext();
            // 移除绑定参数
         	 sc.removeAttribute("song");
        }
    }
    
  • 获取服务器版本号、地址、路径…等等

3、上下文对象案例


案例—获取访问当前请求人数

  • 思路

    来一个 count+1
    来一个 count+1
    所以count必须是是全局变量
    使用上下文绑定
    每次添加的时候判断
    	1、如果count不存在,说明是第一次访问,绑定("count",1)
    	2、直接获得count++
    
  • servlet

    @WebServlet("/count")
    public class CountServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            // 设置响应格式
            resp.setContentType("text/html;charset=utf-8");
            // 获得上下文对象
            ServletContext sc = this.getServletContext();
            // 获得访问的人数
            Object count = sc.getAttribute("count");
            // 如果count==null,说明第一次访问
            if (count==null){
                // 绑定count到上下文
                sc.setAttribute("count",1);
            } else{
                // 先将对象转化为字符串,再把字符串转化为数字
                Integer newCount = Integer.parseInt(count.toString())+1;
                sc.setAttribute("count",newCount);
            }
            // 输出页面
            PrintWriter writer = resp.getWriter();
            writer.println("访问人数为:"+sc.getAttribute("count"));
        }
    }
    
  • 服务器如果没有关,不管在哪个浏览查看,都会是最新的人数,只有 当服务器关闭,才会归0

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值