ServletContext对象

ServletContext 对象

可直接通过 getServletContext() 获取,也可以通过 getServletConfig().geServletContext() 获取。

tomcat在启动时为每个web项目都创建一个 ServletContext 实例,服务器关闭时会销毁。

一个 Web 应用中的所有 Servlet 共享同一个 ServletContext 对象,因此 Servlet 对象之间可以通过 ServletContext 对象来实现通讯。

ServletContext 接口中的部分方法: 

public interface ServletContext{
    Object getAttribute(String var1);
    Enumeration<String> getAttributeNames();
    void setAttribute(String var1, Object var2);
    void removeAttribute(String var1);
}

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

// Demo1 将数据存到 ServletContext 对象中
public class Demo1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "Hello";
        //获得ServletContext对象
        ServletContext context = this.getServletConfig().getServletContext();
        //将data存储到ServletContext对象中
        context.setAttribute("data", data);  
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

// Demo2 从 ServletContext 对象中获取数据
public class Demo2 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //从ServletContext对象中取出数据
        String data = (String) context.getAttribute("data"); 
        response.getWriter().print(data);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

获取 Web.xml 文件中的初始化参数

在 web.xml 文件中使用 <context-param> 标签配置 Web 应用的初始化参数。

<context-param>
    <param-name>name</param-name>
    <param-value>Noteligible</param-value>
</context-param>

通过 ServletContext 对象获取 Web 应用的初始化参数。

public class Demo3 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        // 获取 Web 应用的初始化参数
        String name = context.getInitParameter("name");
        response.getWriter().print(name);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

用 ServletContext 实现请求转发

访问的是 Demo4,使用 ServletContext 将请求转发给 Demo5,浏览器会显示 Demo5 的内容。

public class tDemo4 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //获取请求转发对象RequestDispatcher
        RequestDispatcher RequestDispatcher = context.getRequestDispatcher("Demo5");
        //调用forward方法实现请求转发
        RequestDispatcher.forward(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

public class Demo5 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getOutputStream().write("hello".getBytes());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

在客户端缓存Servlet的输出

对于不经常变化的数据,在servlet中可以为其设置合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能。例如:

public class ServletDemo5 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "Hello World";
        // 设置数据合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能,这里将其设置为1天
        response.setDateHeader("expires",System.currentTimeMillis() + 24 * 3600 * 1000);
        response.getOutputStream().write(data.getBytes());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值