简体   繁体   English

Portlet会话中设置的属性在Servlet会话中不可用

[英]Attribute set in portlet session is not available in servlet session

Post on Liferay Forums: https://www.liferay.com/community/forums/-/message_boards/message/47412302 在Liferay论坛上发贴: 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 我在JSR-286 Portlet中有一个简单的应用程序设置,以从Portlet session.attribute检索值。

doView() method: doView()方法:

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: 我在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): 并在TestServlet.java(相同的包)中检索属性:

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 应用程序的输出返回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. 我已经在tomcat / lib中包含了javax-servlet-api-3.1.0来检索HttpServletRequest类,但还没有发现其他可能缺少的东西。 I also haven't seen any Exceptions/ClassNotFound Errors. 我也没有看到任何Exception / ClassNotFound错误。

Could there be any kind of server configuration that could interfere with the Session? 是否有任何类型的服务器配置可能会干扰会话? (Authentication, network config, security) (身份验证,网络配置,安全性)

Local setup 本地设置

  • Tomcat 7.0.33 的Tomcat 7.0.33
  • jdk-1.7 (compiled with 1.6 and 1.7) jdk-1.7(与1.6和1.7编译)

Remote setup 远端设定

  • Tomcat 7.0.33 的Tomcat 7.0.33
  • Apache Web Server Apache Web服务器
  • jdk-1.6.0u35 JDK-1.6.0u35
  • more jar files in /lib (jdbc drivers, etc) / lib中有更多jar文件(jdbc驱动程序等)

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: 如果要在同一应用程序中的portlet和servlet之间共享会话数据(战争),则必须将属性放在应用程序范围内,如下所示:

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

and then also retrieve it in portlet using scope: 然后还使用范围在portlet中检索它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM