Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: PortletSession doesn't work
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:
<%@ include file="/html/portlet/ext/blogsioc/init.jsp" %>
<%
String author = (String) portletSession.getAttribute("jspcr.psession.blogsioc.author", PortletSession.APPLICATION_SCOPE);
%>
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...
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?

I've wrote this:
FIRST jsp:
<%
PortletRequest pRequest = (PortletRequest)request.getAttribute("javax.portlet.request");
PortletSession pSession = pRequest.getPortletSession();
pSession.setAttribute("jspcr.psession.blogsioc.author", entry.getUserName(), PortletSession.APPLICATION_SCOPE);
%>
SECOND jsp:
<%@ include file="/html/portlet/ext/blogsioc/init.jsp" %>
<%
String author = (String) pSession.getAttribute("jspcr.psession.blogsioc.author", PortletSession.APPLICATION_SCOPE);
%>
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: <%@ include file="/html/portlet/ext/blogsioc/init.jsp" %>
2:
3: <%
4: String author = (String) pSession.getAttribute("jspcr.psession.blogsioc.autho
r", PortletSession.APPLICATION_SCOPE);
5: %>
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
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.
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
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.
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
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...
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.
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.
Powered by Liferay™