留言板

HttpSessoion and PortletSession Example

thumbnail
Jay Trivedi,修改在11 年前。

HttpSessoion and PortletSession Example

Regular Member 帖子: 109 加入日期: 12-11-24 最近的帖子
I simply want to create a an HttpSession and PortletSession and understand differance between them.
Here is the code that i have written but seems not working fine. Any !dea friends.

//This code is written in ProcessAction Class
                HttpServletRequest httpreq = PortalUtil.getHttpServletRequest(actionRequest);
		HttpSession session = httpreq.getSession(true);
		session.setAttribute("LIFERAY_SHARED_INFO", "Jay");

//In Jsp I just want to read the parameter that i have passed. code is

 String name = (String)session.getAttribute("LIFERAY_SHARED_INFO");
 System.out.println("SEssion:"+name);   //gives null why?

But it gives null. Also i have made necessary changes in liferay-portlet.xml
<private-session-attributes>true</private-session-attributes> // I tried both true/false but its still null.

Some one please specify a pure code to get session attribute in action class and read that passed variable in jsp.

Thanks Jay.
thumbnail
Juhi Kumari,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Expert 帖子: 347 加入日期: 11-12-12 最近的帖子
Hi Jay,

I tried your code only and its printing session value on console.
Just check once again.

Regards
Juhi
thumbnail
Jay Trivedi,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Regular Member 帖子: 109 加入日期: 12-11-24 最近的帖子
Hi Juhi,
I tired it again by clearing every thing, seems my compiler is angry on me. can u put down an example, if possible for both Portlet Sessoin and HttpSession.
I tried it with an alternate way.

//In java class
final PortletSession portletSession = actionRequest.getPortletSession();
portletSession.setAttribute("password", "authenticated", PortletSession.APPLICATION_SCOPE);

//In jsp
Object name= (String) session.getAttribute("password");
System.out.println("Value of Pwd Session : "+ name);

again m on ground emoticon Still on null;
Any straight examples ?

Thanks Jay.
thumbnail
Juhi Kumari,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Expert 帖子: 347 加入日期: 11-12-12 最近的帖子
Hi Jay,

Just try to print session id in your action class as well as in jsp, and check they are same or not.

Regards
Juhi
thumbnail
Jay Trivedi,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Regular Member 帖子: 109 加入日期: 12-11-24 最近的帖子
Juhi,
Thanks a lot seems working. now, emoticon

Jay Trivedi.
thumbnail
Jay Trivedi,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Regular Member 帖子: 109 加入日期: 12-11-24 最近的帖子
Got the solution emoticon



//In java class
final PortletSession portletSession = actionRequest.getPortletSession();
portletSession.setAttribute("password", "authenticated", PortletSession.APPLICATION_SCOPE);

//In jsp
Object name= (String)[color=#f45050] PortletSession[/color].getAttribute("password");
System.out.println("Value of Pwd Session : "+ name);
thumbnail
David H Nebinger,修改在11 年前。

RE: HttpSessoion and PortletSession Example (答复)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Jay Trivedi:
I simply want to create an HttpSession and PortletSession and understand differance between them.


I'll save you the trouble...

HttpSession is a session object managed by the application container. Each visitor will have a unique session object per web application they are invoking in the application container.

PortletSession is a similar concept, but different implementation details. The ROOT servlet (Liferay) maintains a separate portlet session for every portlet on a page for the user that they have viewed. You view a page with 4 portlets on it, you basically have 4 portlet sessions for yourself. Portlets can access their session data, but it is separate from the portlet session of another portlet, another page, another user, etc.

Just as HttpSession is a place for web apps to store their own data that they want to make available during user sessions, PortletSession is the same way. A single portlet can store it's own data to make it available during user sessions.

Now that's how it works in it's pure form. Liferay allows for some customization of how that works in order to share portlet session data, etc., but the use cases for those kinds of things are really limited. If you find yourself wanting to tinker with these settings, I think you're trying to hack something in that should really have been resolved in some other cleaner fashion.

Another twist: Normally your plugins are separate wars, so there are often times when you are looking at two different HttpSessions (one managed for the ROOT web app and one for the web app that actually drives your plugin). These sessions are not the same, and something you store in one is not visible in the other.

I think the primary part of your problem is not understanding what 'session' is in a JSP page rendered within a portlet. A portlet JSP page, the session object is the portlet session, not the web application session.

This is really an example of why you should not be muddling with session data directly. It's very easy to lose sight of which scope you're actually in and what 'session' represents in each context.
thumbnail
Jay Trivedi,修改在11 年前。

RE: HttpSessoion and PortletSession Example

Regular Member 帖子: 109 加入日期: 12-11-24 最近的帖子
Thanks David, This content provides me a clear vision for action of data with in specified contexts. emoticon

Jay.
thumbnail
kailash b,修改在11 年前。

RE: HttpSessoion and PortletSession Example

New Member 帖子: 14 加入日期: 12-11-12 最近的帖子
Thanks David H Nebinger,

Its very clear definition for portlet session...
Debopam Mitra,修改在9 年前。

RE: HttpSessoion and PortletSession Example

Junior Member 帖子: 39 加入日期: 12-9-27 最近的帖子
Thanks David for sharing this important concept.
thumbnail
Lucas Fragomeni,修改在9 年前。

RE: HttpSessoion and PortletSession Example

New Member 帖子: 4 加入日期: 14-9-12 最近的帖子
David H Nebinger:
This is really an example of why you should not be muddling with session data directly. It's very easy to lose sight of which scope you're actually in and what 'session' represents in each context.


Ok.. but what if I need to manage some attribute related to the user session as a whole, for instance, an attribute to map if the user wants to view the "Mobile Version" or the "Desktop Version" (which is my case). Is there a way to access the Session from within a MVCPortlet?
thumbnail
David H Nebinger,修改在9 年前。

RE: HttpSessoion and PortletSession Example

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
you mean just switching modes, not sharing a session from two different logons on two different devices, right?

Also is it one portlet that has both mobile and desktop views, or are they two different portlets?

Ultimately trying to use a session value gets difficult to control. I mean, you can use the portal's HTTP session in many cases, but this can break if you have some web service servlet(s) in your war that needs access to the session, well it has no visibility to the portal's session because the call is being handled by the servlet.

At the end of the day, we must all realize session data for how we all use it - a temporary data store to cache info that we don't want to persist. It is extremely easy to misuse. Session data has one of the biggest impact on how many concurrent users you can support (more session data per login means you'll hit a memory ceiling long before you top out on cpu or other resources). In clusters, session data needs to be replicated to the other nodes to allow for transparent node switch after a node fails, so the more you store in the session, you take a bigger hit transferring that data to the nodes and keeping them in sync).

Any example one might make for storing data in the session, you can usually identify one or more alternate ways to keep the data. And often times it is better to use these alternate methods since anything stored in the session needs to be replicated when in clusters. In fact, most issues related to data in the cluster are due to session data replication.

Other viable alternatives include storing a doc in the index (in the lucene index, for example). These docs can be indexed by user id (or whatever), are pretty fast to access, and they are available to all of the nodes in the cluster.

There's also database storage, either persistent or even HSQL. Yes, that's right, I said HSQL. I mean, if you're storing data in the session cuz you don't care what happens to it if the node fails, well then HSQL is a viable solution in the same way. Use a startup action to purge the HSQL table(s) and you have your cluster-friendly temporary data store that doesn't have session replication issues.

These and other options (cookies, messaging via LMB, etc.) can all serve the same purposes that we use sessions for, but they don't have the typical issues that session data has. Choosing between the options really depends upon requirements, but they can all be viable options.
thumbnail
Lucas Fragomeni,修改在9 年前。

RE: HttpSessoion and PortletSession Example

New Member 帖子: 4 加入日期: 14-9-12 最近的帖子
First of all, thanks for the answer!

I've been developing web applications for quite a while, and I'm well aware of the implications of controlling session attributes, so, that is not the problem for me.

What I'm trying to understand about Liferay is: The concept of a "Global Session" per user has been removed? Is it a different paradigm, considering the standard servlet scopes (application, session, request, response, page)?

Is this because of the use of different WARs that the session can't be shared among them, or is it a Liferay option to abstract the session and change it's standard meaning?

I could some of the options you suggested, but I'm used to using the Session scope for some purposes. So, if that is not the "Liferay Way", I can adapt. Lol...