Portlet会话中设置的属性在Servlet会话中不可用 [英] Attribute set in portlet session is not available in servlet session

查看:135
本文介绍了Portlet会话中设置的属性在Servlet会话中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Liferay论坛上发帖: https://www.liferay.com /community/forums/-/message_boards/message/47412302

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

我在JSR-286 portlet中有一个简单的应用程序设置,可以从Portlet session.attribute

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

doView()方法:

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);
   }

我在这里设置属性 TestPortlet.java:

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);
    }
}

并在TestServlet.java(相同的包)中检索属性:

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);
}

应用程序的输出返回null

The output of the application returns null

Setting Attribute in Portlet
Test Attribute from Servlet:null

输出应为:

Test Attribute from Servlet:TESTING SESSION

该应用程序 可以在我的本地设置上运行 ,但是不能在配置几乎相同的远程服务器上使用. 我已经在tomcat/lib中包含了javax-servlet-api-3.1.0来检索HttpServletRequest类,但是还没有发现其他可能缺少的东西.我也没有看到任何Exception/ClassNotFound错误.

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)

本地设置

  • Tomcat 7.0.33
  • jdk-1.7(由1.6和1.7编译)

远程设置

  • Tomcat 7.0.33
  • Apache Web服务器
  • jdk-1.6.0u35
  • /lib中有更多jar文件(jdbc驱动程序等)

推荐答案

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

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); 

,然后还使用范围在portlet中检索它:

and then also retrieve it in portlet using scope:

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

这篇关于Portlet会话中设置的属性在Servlet会话中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆