zoukankan      html  css  js  c++  java
  • ServletContext对象

    1. ServletContext对象代表整个web应用

    2. ServletContext对象是一个域对象(可以存储数据的对象)

    3. ServletContext对象的内部维护了一个map集合 key是String类型 value是Object类型


    1. 将ServletContext作为域对象 实现多个Servlet对象共享案例

    // 存数据

    // 1. 获得ServletContext对象

    ServletContext context = getServletContext();

    // 2. 存入域 

    context.setAttribute("name",Object) ;

    // 3. 取数据 

    Object obj = (Object)getServletContext().getAttribute("name");

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

    getContext().getInitParameter("name") ;

    3. 统计一个web应用的访问量

    在 context 域中维护一个 count 变量

    ServletContext context = getServletContext();

    Integer count = (Integer) context.getAttribute("count");

    if(count==null) count=0;

    count++;

    context.setAttribute("count",count) ;

    response.getWriter().writer("count=" + count) ;

    4. 实现Servlet转发 

    ServletContext context = getServletContext();

    RequestDispatcher dispatcher = context.getRequestDispatcher("/a.jsp");

    dispatcher.forward(request,response);

  • 相关阅读:
    NSPrediccate 查询
    集合 不可变集合
    集合 不可变
    考核题 7
    考核题 6
    考核题 4
    练习题12
    练习题3
    iOS 实现在string任意位置添加新的表情
    在 ZBarSDK 中使用Block回调传值 Block在扫描成功后 变为空
  • 原文地址:https://www.cnblogs.com/Knuth/p/2474330.html
Copyright © 2011-2022 走看看