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.
意涵:同一用戶操作三個屬於不同 Portlet App 的 Portlet,將產生三個獨立的 Portlet Session (加上 Portal Session 共四個)。
Portlets, servlets and JSPs are bundled in an extended web application called portlet application. Portlets, servlets and JSPs within the same portlet application share class loader, application context and session.
進一步闡明:Portlet Session 必須將屬性儲存在該 Portlet App 的 HttpSession 中。Portlet、Servlet、JSP 可透過各自 Session 介面互通數據。任一 Session 失效,另一個必須同步失效。
PortletSession session = request.getSession(true);
URL url = new URL("https://www.foo.com");
session.setAttribute("home.url", url, PortletSession.APPLICATION_SCOPE); // 跨 Portlet 共享
session.setAttribute("bkg.color", "RED", PortletSession.PORTLET_SCOPE); // 單 Portlet 私有
兩大作用域:APPLICATION_SCOPE (同 Web App 內可見) / PORTLET_SCOPE (僅當前 Portlet 可見)。