Java Servlet Session 里面应该包含什么以及重定向

发布于 12-28 13:02 字数 268 浏览 1 评论 0原文

我想知道哪些数据应该进入会话。假设我有一个用户对象,其中包含表示图像链接的对象列表。我应该在会话中保存用户对象的用户名并使用它来查询数据库中的用户对象以及图像的链接,还是应该保存整个对象、会话中包含的列表并在没有更新的情况下使用它需要吗?这是一个糟糕的策略吗?

我对此很陌生,所以如果这没有任何意义,我很抱歉。如果 requestdispatcher 代替了 sendredirect,是否可以执行encodeurl?看起来如果我使用请求调度程序,我必须在 cookie 中发送 jessesion id。

I was wondering what data should go into a session. Say I had a user object that had a list of objects that represent links to images. Should I save the username of the user object in the session and use that to query the db for the user object with the links to the images, or should I save the entire object, list included in the session and use that if no updates are needed? Is this a bad strategy?

I am new at this so I am sorry if this does not make any sense. Also is it possible to do encodeurl if the requestdispatcher is being insteand of sendredirect? It seems like if I use the request dispatcher I have to send the jessesion id in the cookie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜2025-01-04 13:02:39

在会话中存储完整的对象图:

优点:

  • 更快 - 所有内容都始终在内存中,只要它适合
  • 更简单 - 您正在使用对象,无需额外的数据库交互需要层

缺点:

  • 资源消耗 - 您的会话将消耗大量资源,尤其是在对象图很大的情况下。一个拥有数千张图像的用户将使您的服务器爬行
  • 可扩展性 - 通过易于序列化的对象来分发小型会话要容易得多
  • 一致性 - 当数据库发生更改时您必须确保 HTTP 会话也已更新。这可能会很痛苦,

话虽如此,您应该保持 HTTP 会话尽可能小,并且当性能成为问题(频繁的数据库查询)时考虑缓存。

Storing the full object graph in session:

Pros:

  • Faster - everything is always in memory, as long as it fits there
  • Easier - you are working with objects, no additional database interaction layer is needed

Cons:

  • Resource consumption - your sessions will consume a lot of resources, especially if object graph is huge. One user having thousands of images will make your server crawl to its knees
  • Scalability - it is much easier to distribute small sessions with easy to serialize objects
  • Consistency - when the database is changed you must make sure that the HTTP session is updated as well. This can be painful

That being said you should keep the HTTP session as small as possible and when performance becomes a problem (frequent database queries) think about caching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文