Attribute set in portlet session is not available in servlet session

跟風遠走 提交于 2019-12-11 07:49:51

问题


Post on Liferay Forums: https://www.liferay.com/community/forums/-/message_boards/message/47412302

I have a simple application setup within a JSR-286 portlet to retrieve the value from a Portlet session.attribute

doView() method:

public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
     throws PortletException, IOException
   {
     renderResponse.setContentType("text/html");
     getFormBean(renderRequest.getPortletSession());
     PortletURL renderUrl = renderResponse.createRenderURL();
     renderUrl.setWindowState(WindowState.MAXIMIZED);
     PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(this.viewUrl);
     dispatcher.include(renderRequest, renderResponse);
   }

I set my attribute here in TestPortlet.java:

private void getFormBean(PortletSession session)
{
    String testVar = (String)session.getAttribute("testAttr", 1);
    if (null == testVar) {
        System.out.println("Setting Attribute inside Portlet");
        session.setAttribute("testAttr", "TESTING SESSION", 1);
    }
}

And retrieve the attribute here in TestServlet.java (same package):

private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
         String testVal = (String) request.getSession(true).getAttribute("testAttr");
         System.out.println("Test Attribute from Servlet:"+testVal);
}

The output of the application returns null

Setting Attribute in Portlet
Test Attribute from Servlet:null

Output should be:

Test Attribute from Servlet:TESTING SESSION

This application does work on my local setup, however not on a remote server with almost the same configurations. I've included the javax-servlet-api-3.1.0 in my tomcat/lib to retrieve the HttpServletRequest Class, haven't found what else could be missing. I also haven't seen any Exceptions/ClassNotFound Errors.

Could there be any kind of server configuration that could interfere with the Session? (Authentication, network config, security)

Local setup

  • Tomcat 7.0.33
  • jdk-1.7 (compiled with 1.6 and 1.7)

Remote setup

  • Tomcat 7.0.33
  • Apache Web Server
  • jdk-1.6.0u35
  • more jar files in /lib (jdbc drivers, etc)

回答1:


If you want to share session data between portlet and servlet in the same application (war), you have to place the attribute in the application scope, like this:

portletSession.setAttribute("testAttr", "TESTING SESSION", PortletSession.APPLICATION_SCOPE); 

and then also retrieve it in portlet using scope:

portletSession.getAttribute("testAttr", PortletSession.APPLICATION_SCOPE);


来源:https://stackoverflow.com/questions/27807222/attribute-set-in-portlet-session-is-not-available-in-servlet-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!