Request,Session和ServletContext中的属性之间的差异 [英] Differences between attributes in Request, Session and ServletContext

查看:91
本文介绍了Request,Session和ServletContext中的属性之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解这三种设置属性的方式之间的区别:

I am having trouble understanding the differences between these 3 ways of setting attributes:

// String as attribute of request
req.setAttribute("name", "Sluggo");

// Integer as attribute of session
req.getSession().setAttribute("age", 10);

// Date as attribute of context
getServletContext().setAttribute("today", new Date());

  1. 有什么区别?
  2. 何时应使用它们?

推荐答案

这三个具有不同的范围:

Those three have different scopes:

  • 请求属性在该请求/响应周期的生命周期内有效

  • request attributes live for the life of that request/response cycle

该会话有效期内的会话属性

session attributes for the life of that session

ServletContext跨servlet上下文,并且一直存在直到上下文被销毁为止.

ServletContext is across the servlet context and it lives until context gets destroyed.

这篇关于Request,Session和ServletContext中的属性之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆