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

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

问题描述

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

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天全站免登陆