RE: PortletSession doesn't work

1165624, modified 16 Years ago. Junior Member Posts: 47 Join Date: 8/9/08 Recent Posts
Hi,
I have to set a session variable so that I can see it from two different jsp files of the same portlet. One of these jsp files is opened into a liferay.popup. I set the variable into the FIRST file using:


<%
portletSession.setAttribute("jspcr.psession.blogsioc.author", entry.getUserName(), PortletSession.APPLICATION_SCOPE);
%>


into the onclick valude of: <a href="........" onclick="...(here)..."></a>

Into the SECOND file I have wrote only this rows:


&lt;%@ include file="/html/portlet/ext/blogsioc/init.jsp" %&gt;
&lt;%
String author = (String) portletSession.getAttribute("jspcr.psession.blogsioc.author", PortletSession.APPLICATION_SCOPE);
%&gt;


When I click the button, the SECOND jsp is opened into the liferay.popup, but I have this error:


18:31:14,562 ERROR [[jsp]:253] Servlet.service() for servlet jsp threw exception

java.lang.NullPointerException


I use Liferay 5.1.0 (boundled withTomcat).
I'm trying to resolve it since a week...
thumbnail
32141, modified 16 Years ago. Junior Member Posts: 59 Join Date: 4/3/07 Recent Posts
<%
portletSession.setAttribute("jspcr.psession.blogsioc.author", entry.getUserName(), PortletSession.APPLICATION_SCOPE);
%>

<%
String author = (String) portletSession.getAttribute("jspcr.session.blogsioc.author", PortletSession.APPLICATION_SCOPE);
%>


do you see the psession and the session difference?
1165624, modified 16 Years ago. Junior Member Posts: 47 Join Date: 8/9/08 Recent Posts
Yes sorry, I've seen it. I have modified the text... the error is about the code without that error.
thumbnail
32141, modified 16 Years ago. Junior Member Posts: 59 Join Date: 4/3/07 Recent Posts
where is portletSession decleared?
1165624, modified 16 Years ago. Junior Member Posts: 47 Join Date: 8/9/08 Recent Posts
I haven't decleared it...Should I?
thumbnail
32141, modified 16 Years ago. Junior Member Posts: 59 Join Date: 4/3/07 Recent Posts
are you sure entry.getUserName() is not null?
1165624, modified 16 Years ago. Junior Member Posts: 47 Join Date: 8/9/08 Recent Posts
Yes, I am... emoticon
thumbnail
32141, modified 16 Years ago. Junior Member Posts: 59 Join Date: 4/3/07 Recent Posts
try request.getPortletSession();
1165624, modified 16 Years ago. Junior Member Posts: 47 Join Date: 8/9/08 Recent Posts
What do you mean?
I've wrote this:

FIRST jsp:

&lt;%
	PortletRequest pRequest = (PortletRequest)request.getAttribute("javax.portlet.request");

	PortletSession pSession = pRequest.getPortletSession();
			
	pSession.setAttribute("jspcr.psession.blogsioc.author", entry.getUserName(), PortletSession.APPLICATION_SCOPE);
%&gt;


SECOND jsp:

&lt;%@ include file="/html/portlet/ext/blogsioc/init.jsp" %&gt;

&lt;%
String author = (String) pSession.getAttribute("jspcr.psession.blogsioc.author", PortletSession.APPLICATION_SCOPE);
%&gt;


But now I get this error:

Error in the SECOND file:

An error occurred at line: 4 in the jsp file: /html/portlet/ext/blogsioc/view_si
oc.jsp
pSession cannot be resolved
1: &lt;%@ include file="/html/portlet/ext/blogsioc/init.jsp" %&gt;
2:
3: &lt;%
4: String author = (String) pSession.getAttribute("jspcr.psession.blogsioc.autho
r", PortletSession.APPLICATION_SCOPE);
5: %&gt;
Apurb Kumar Choudhary, modified 7 Years ago. New Member Posts: 13 Join Date: 11/3/14 Recent Posts

Hi,

I am using liferay 6.2 and I am trying to transfer value from hooks to portlet using Portlet Session, but I am getting null at portletSession.getAttribute.
 

At the hooks side inside processAction:

public void processAction(StrutsPortletAction originalStrutsPortletAction,
   PortletConfig portletConfig, ActionRequest actionRequest,
   ActionResponse actionResponse){

  PortletSession session = actionRequest.getPortletSession();

  session.setAttribute(("LIFERAY_SHARED_attribute_name","asdasda", PortletSession.APPLICATION_SCOPE);

}

and at the portlet side in processAction method:

public void createWorkItem(ActionRequest actionRequest,
   ActionResponse actionResponse) {

  PortletSession session = actionRequest.getPortletSession();

  String attributeValue = (String) session.getAttribute("LIFERAY_SHARED_attribute-name", PortletSession.PORTLET_SCOPE);

}

In liferay-portlet.xml

<instanceable>false</instanceable>
<private-session-attributes>false</private-session-attributes>
<requires-namespaced-parameters>false</requires-namespaced-parameters>

 

At the portlet side I am getting null.

 

Please help.

 

Thanks,

Apurb Choudhary

thumbnail
Victor Zorin, modified 7 Years ago. Liferay Legend Posts: 1228 Join Date: 4/14/08 Recent Posts

first, check for attribute name typing mistake,  you have:

"LIFERAY_SHARED_attribute_name"

vs

"LIFERAY_SHARED_attribute-name"

 

if that's not the cause, you can use PortalUtil to narrow down to original servlet requests and print out all allrtibutes and see what is populated.

Also log the sequence of attribute population, making sure that they are coming one after another.

Apurb Kumar Choudhary, modified 7 Years ago. New Member Posts: 13 Join Date: 11/3/14 Recent Posts

Really sorry about the typo.

 

Please consider the fact that both having same name i.e.attribute_name and scope is APPLICATION_SCOPE.

 

Can you explain how I can use this portletUtil here.

 

Thanks,

Apurb Choudhary

 

thumbnail
Victor Zorin, modified 7 Years ago. Liferay Legend Posts: 1228 Join Date: 4/14/08 Recent Posts

It is PortalUtil, located in the following package:

import com.liferay.portal.util.PortalUtil;
 

In Liferay there is like a stacked hierarchy of visibility of attributes, the PortalUtil allows you to  check the data at all levels, essentially by providing the 'casting' mechanism, like in the following code:

import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
 

methodName(PortletRequest portletRequest)

{

                HttpServletRequest hrequest = PortalUtil.getHttpServletRequest(portletRequest);
                HttpServletRequest frequest = PortalUtil.getOriginalServletRequest(hrequest);
}

Check the methods available in PortalUtil, it is quite useful class. So what I was suggesting before is that print all session attributes at portletRequest, hrequest, frequest and check the difference in this list.

 

In your code you add the session attribute at one level and are trying to obtain it at a different level. When you log the attributes at each level, you will hopefully be able to understand what is going on.

Apurb Kumar Choudhary, modified 7 Years ago. New Member Posts: 13 Join Date: 11/3/14 Recent Posts

I did double check at the hook level and found that the value is not getting stored in session itself.

So that's the reason I am not getting value in the portlet level, but the question remains same why the value is not setting in PortletSession.

 

Am I doing anything wrong.

 

Thanks,

Apurb Choudhary

thumbnail
Victor Zorin, modified 7 Years ago. Liferay Legend Posts: 1228 Join Date: 4/14/08 Recent Posts

print out sessionIds when you set the attribute and when you get it.

If they are different, the session is reset/changed between two calls, that could be for example that the hook is invoked  when user is anonymous and read the session attributes when user is signed in, etc. Depends on your lifecycle/business logic...

Apurb Kumar Choudhary, modified 7 Years ago. New Member Posts: 13 Join Date: 11/3/14 Recent Posts

Yes you are right the session id is different at both place. Well in that case can you suggest how to tackle this situation.

 

My agenda is to transfer a object from hook to portlet. in the hook I have actionRequest and actionResponse available to use. 

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Apurb Kumar Choudhary:

Yes you are right the session id is different at both place. Well in that case can you suggest how to tackle this situation.

Assuming the session IDs are different due to a login that happens in-between, you'll need to tell Liferay that it's a session attribute that needs to persist through login.

    #
    # Set a comma delimited list of attribute names that will be copied to the
    # new session when the property "session.enable.phishing.protection" is set
    # to true.
    #
    session.phishing.protected.attributes=CAS_LOGIN,HTTPS_INITIAL,LAST_PATH,OPEN_ID_CONNECT_SESSION

If there is a different reason for the session ID change, you'll need to tell us what you're doing in between the time your code sets the shared session attribute and when you visit your portlet.