Liferay Portal中的Session

PortletSession objects must be scoped at the portlet application context level. Each portlet application has its own distinct PortletSession object per user session. The portlet container must not share the PortletSession object or the attributes stored in it among different portlet applications or among different user sessions

在PortletAction中
PortletSession session = renderRequest.getPortletSession();
		java.util.Map<String,Object> map = session.getAttributeMap();
		//打印出所有的session
		for(String s : map.keySet()){
			System.out.println(s+":"+map.get(s));
		}
		String username = (String)session.getAttribute("user");
		System.out.println(username);


再看
PortletSession session = renderRequest.getPortletSession();
		java.util.Map<String,Object> map = session.getAttributeMap(PortletSession.APPLICATION_SCOPE);
		//打印出所有的session
		for(String s : map.keySet()){
			System.out.println(s+":"+map.get(s));
		}
		String username = (String)session.getAttribute("user",PortletSession.APPLICATION_SCOPE);
		System.out.println(username);


上面2个 唯一区别是:获取属性的方法多了PortletSession.APPLICATION_SCOPE。每个Portlet Application应该在Process Action和Render的时候应该拥有自己私有的Session对象,来为当前用户服务。也就是说,如果某一个用户在一个PORTAL系统中操作不同的PORTLET,而且这些PORTLET属于不同的PORTLET APPLICATION,则,这个用户将操作多个SESSION对象,用户在每个PORTLET中操作SESSION时,这个SESSION都将是属于该PORTLET所在的上下文的私有SESSION 对象。

如果我要获得全局的用户session,怎么办?比如要获取CAS的用户信息。CAS用户信息应该放在全局的HttpSession中,那LifeRay可进行如下操作:
//获取HttpSession,PortalUtil 在 portal-service下的
//com.liferay.portal.util.PortalUtil
		HttpSession session = PortalUtil.getHttpServletRequest(renderRequest)
		.getSession();
String username = (String) session
				.getAttribute("edu.yale.its.tp.cas.client.filter.user");


可以看看PortalUtil中是怎么实现获取全局Session
//PortalUtil 中的实现方法
return getPortal().getHttpServletRequest(portletRequest);
//getPortal返回的Portal的实现类
//在portal-impL下的com.liferay.portal.util.PortalImpl
//它的实现
if (portletRequest instanceof PortletRequestImpl) {
			PortletRequestImpl portletRequestImpl =
				(PortletRequestImpl)portletRequest;

			return portletRequestImpl.getHttpServletRequest();
		}
		else if (portletRequest instanceof PortletRequestWrapper) {
			PortletRequestWrapper portletRequestWrapper =
				(PortletRequestWrapper)portletRequest;

			return getHttpServletRequest(portletRequestWrapper.getRequest());
		}

		HttpServletRequest request = HttpServletUtil.getHttpServletRequest(
			portletRequest);

		if (request != null) {
			return request;
		}

		throw new RuntimeException(
			"Unable to get the HTTP servlet request from " +
				portletRequest.getClass().getName());

你可能感兴趣的:(java,servlet)