将数据保存到JSF中的会话 [英] Saving data to session in JSF

查看:104
本文介绍了将数据保存到JSF中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是J(2)EE和Web应用程序开发领域的新手,但是我很快就绕过它了,学到了很多东西.每天对我来说都是一次奇妙的新发现之旅.

I am new to the world of J(2)EE and web app development but am quickly navigating my way around it and learning a lot. Every day is a fantastic voyage of new discovery for me.

我目前正在一个项目中,我正在Glassfish v2上使用Visual JSF Woodstock.我对JSF也很陌生.

I am currently working on a project in which I am using Visual JSF Woodstock on Glassfish v2. I am pretty new to JSF also.

有时候,我需要在请求之间保存一些对象(例如MyObject).从到目前为止的阅读和理解中,我需要使用会话在不同请求之间保存这些对象.到目前为止一切顺利.

There are times when I need to save some objects (say MyObject for instance) between requests. And from what I have read and understood so far, I need to be using sessions to save these objects between different requests. So far so good.

这正是我关心的问题.我知道在JSP中您可以使用session.setAttribute("myObj", myObject),它将使用cookie或url重写或隐藏的表单变量在客户端保存对象.

Exactly how to do this is where my concern lies. I know that in JSP you can use the session.setAttribute("myObj", myObject) which would save the object at client side using cookies or url rewrites or hidden form variables.

另一方面,在JSF中,我使用Session作用域的bean,例如使用SessionBean1,并将对象另存为SessionBean1属性(例如SessionBean1.setSomeOjb(myObj)).这是正确的方法吗?

On the other hand, in JSF I use Session scoped beans, say SessionBean1 for e.g., and save objects as SessionBean1 properties (e.g. SessionBean1.setSomeOjb(myObj)). Is this the right way to go about with this?

我猜想这样做会导致服务器端的内存利用率提高,因为每个请求都将创建一个新的会话作用域bean实例SessionBean1以及SessionBean1中保存的myObject实例使用的内存.

I am guessing that doing it this way will result in increased memory utilization at the server end since each request will create a new instance of the session scoped bean, SessionBean1 plus the memory utilized by the saved myObject instances in SessionBean1.

我已阅读到可以使用FacesContext.getExternalContext().getSession/getSessionMap()它将在客户端保存会话变量.

I have read that you can use FacesContext.getExternalContext().getSession/getSessionMap() which would save session variables at client side.

那么您建议我使用哪种方法-会话范围的bean或sessionmap来保存对象以在会话请求之间进行访问?

So which method would you suggest that I use - the session scoped bean or the sessionmap to save objects for access between requests for a session?

谢谢.

推荐答案

通常,Java EE Web Apps倾向于不希望在客户端保存会话数据.您应该担心服务器端的会话膨胀,常见的问题是会话占用空间很大,这可能会导致严重的资源和性能问题,尤其是在集群环境中.

In general Java EE Web Apps tend not to expect to save session data client side. You're right to be concerned about session bloat on the server side, a common problem seen is to have huge session footprints which can cause significant resource and performance issues, and can be especially in clustered environments.

我想知道你在哪里

我已阅读到您可以使用FacesContext.getExternalContext().getSession/getSessionMap()来将会话变量保存在客户端.

I have read that you can use FacesContext.getExternalContext().getSession/getSessionMap() which would save session variables at client side.

我相信(在这一点上纠正我),这只是允许访问HttpSession对象,然后您可以在其上使用相同的对象

I believe (correct me on this point) that this simply gives access to the HttpSession object, on which you can then use the same

 session.setAttribute("myObj", myObject)

这本身并不会将对象发送回客户端,而是保存在服务器中,并由通常通过cookie传递的某些会话标识符作为关键字.

this does not in itself send the object back to the client, it's held in the server and keyed by some session identifier, usually passed in a cookie.

现在有另外两种技术:您可以明确地选择将数据放入自己生产的cookie中-可以从JSF或JSP访问的servlet API可以使您做到这一点,或者可以在自己的隐藏字段中使用表单,并因此传递aorund会话数据.

Now there are two other techniques: you could explicitly choose to put data into a cookie of your own manufacture - the servlet APIs that you can access from JSF or JSP would let you do that, or you can use hidden fields on your forms, and hence pass aorund session data.

但是考虑一下.我使用的App Server的经验法则是HttpSession大约为1k-4k不太可能是一个问题.更大的容量(我已经看到以兆字节为单位的会话数)确实给基础架构带来了压力.如果您担心如此大的会话,您是否希望在每次请求时都将Cookie或隐藏字段中的兆字节数据发送回浏览器?甚至1k-2k也可能有点大.

But consider this. A rule of thumb on the App Server I use is that HttpSession of the order of 1k-4k tend not to be a problem. Larger than that (and I have seen sessions of measured in megabytes) do stress the infrastructure. If you were concerned about sessions of that size would you expect to send megabytes of data in a cookie or hidden field back to the browser on every request? Even 1k-2k is probably a bit big.

所以建议:

  1. 保持简单.使用会话API或其JSF表现形式.

  1. Keep it simple. Use the Session API, or its JSF manifestation.

保持会话中受控制的数据量.

Keep the amount of data in the session under control.

为回答有关聚类的问题而添加:

Added in response to question about clustering:

通常,在群集环境中,我们具有会话亲缘关系,因此请求可以发送回相同的群集成员.但是,当请求转到另一台服务器时,我们仍然需要考虑这种情况(也许集群成员失败了).

Typically, in a clustered environment we have session affinity, so that requests are sent back to the same cluster member. However we still need to consider the case (perhaps if a cluster members fails) when the request goes to a different server.

一些App Server供应商通过直接服务器间通信或通过将会话持久化到数据库来提供会话复制-显然这里存在开销,因此有时对于低价值会话,我们只接受在发生以下情况时丢失会话失败.

Some App Server vendors offer session replication, either via direct inter-server communication or by persisting the session to a database - obviously there are overheads here, so sometimes, for low value sessions we just accept the loss of session in event of failure.

有一个论点是,如果会话数据具有很高的价值,则应由应用程序将其持久化,它实际上是业务数据,应将其视为此类数据.为此越来越多地使用诸如Cloudant或MongoDb之类的NOSQL数据库.在这种情况下,我们可以将HTTP会话视为缓存,因为在发生错误的情况下可以检索会话数据.

There is an argument that if the session data has high value then it should be persisted by the application, it's actually business data and should be treated as such. Increasingly, NOSQL databases such as Cloudant or MongoDb are used for this. In this case we may think of the HTTP session as a cache, in the knowledge that the session data can be retrieved in the event of error.

因此,我认为购物车可能对企业具有相当大的价值;它代表了客户想要花钱的周到积累.因此,应将其保留,而不是仅保留在会话中.一旦决定坚持下去,我们就会发现它会导致其他有趣的情况,例如跨许多客户端设备的整合体验.客户开始在台式PC上在家购物,但在线完成购买.

So I'd argue that a Shopping Cart may well have considerable value to the business; it represent the customers thoughtful accumulation of things they want to spend money on. So it should be persisted, rather then just kept in the session. Once we decide to persist it, then we find that it leads to other interesting scenarios such as a consolidated experience across many client devices. The customer starts shopping at home on a desktop PC, but completes the purchase online.

进一步的原则:

3).不要仅仅因为HTTP会话就在那儿而过度使用它.考虑数据的业务价值以及是否应保留.

3). Don't over-use the HTTP session just because it's there. Consider the business value of the data and whether it should be persisted.

这篇关于将数据保存到JSF中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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