Portal Open Source Implementation-liferay portlet session processing (2)

Source: Internet
Author: User
Tags object comments session id return string
Session
Ii. the realization of Liferay

Liferay when building Actionrequestimpl and Renderrequestimpl, a portlet session is set, as shown in the following code: public Renderrequestimpl ( HttpServletRequest req, Portlet portlet,        cacheportlet cachePortlet,         Portletcontext portletctx,        WindowState WindowState, Portletmode portletmode,        PortletPreferences Prefs, String layoutid) {   ...   _req = Dynamicreq;  _portlet = Portlet;  _ Cacheportlet = Cacheportlet;  _portalctx = new Portalcontextimpl ();   _portletctx = PortletCtx;   _windowstate = Windowstate;  _portletmode = Portletmode;  _prefs = prefs;   _ses = new Portletsessionimpl (   _req.getsession (), _portletname, _portletctx);   ... &NBSP} from the blue section (  _ses = new Portletsessionimpl (_req.getsession (), _portletname, _portletctx); ) We can see that this POrtlet session is actually the session object of portal system. So either request call GetSession () or getportletsession () will get the session object for the portal system, regardless of whether the portlet  is or is not part of the portal systems context. And even portlets with different portal application will use the same Session object (portal system). That is, for a portlet, if there is an action on its session, it does not actually operate in the session in the context of the application, but rather in the session in the context of the portal system.

and Liferay provides getportletsession to get the Portletsession object, not the GetSession () method, so even if getportletsession () can get the correct session object, Developers are not allowed to use getsession () because of their habits.

Also, if you call Request.getsession (TRUE), an error may occur because Liferay calls the Portletrequestdispatcherimpl.include () method when it contains a portlet content. This method will generate Portletservletrequest and Portletservletresponse, see the following code:

Portletservletrequest portletservletreq = new Portletservletrequest (Httpreq, Reqimpl, PathInfo, queryString, RequestUR I, Servletpath);

Portletservletresponse portletservletres = new Portletservletresponse (Resimpl.gethttpservletresponse (), ResImpl); The Portletservletrequest constructor is defined as follows: Public Portletservletrequest (HttpServletRequest req, Renderrequest renderrequest, String PathInfo, String querystring, String RequestUri, String servletpath) {

Super (req);

_ses = Req.getsession ();  _renderrequest = Renderrequest;  _pathinfo = PathInfo;  _querystring = querystring;  _requesturi = RequestUri; _servletpath = Servletpath; Therefore, the session is still the context of the portal system. Then the problem is here, Portletservletrequest implements the GetSession () method, but does not implement the GetSession (Boolen Create) method, if the user calls GetSession (true) at this stage , and in some cases it throws NullPointerException

For reasons, see the following code (please note the comment section I added)//applicationhttprequest:

Public HttpSession getsession (Boolean create) {

if (crosscontext) {//There cannot is a session if no context has been assigned yet if (context = null) return (NULL);

           //Return of the current session if it exists and is Vali d            if (session!= null)                  return (Session.getsession ());     //My comments: This will get the session object for the portal system.             HttpSession other = Super.getsession (false);             if (create && (other = null) {                //I-Create a session in the Context:the problem is                //That's the top level request are the only one which can          &nbsp ;     //Create the cookie safely                other = Super.getsession (true);           }             if (other!= null) {                 Session localsession = null;                 Try {                    //My comments: This context did not have the session With the session ID. It can just be found in the portal                    //context. So here it'll return a null value.                   &nBsp Localsession =                         Context.getmanager () findsession (Other.getid ());                     Localsession.access (); My comment: Here, the localsession is null. So it throws a nullpointexception.                } catch (IOException e) {                    //ignore                }                 if (localsession = null) {                     localsession = Context. GetManager (). Createemptysession ();                     Localsession.setnew (True);                     Localsession.setvalid (True);                     Localsession.setcreationtime (System.currenttimemillis ());                     localsession.setmaxinactiveinterval                          (Context.getmanager (). Getmaxinactiveinterval ());                     Localsession.setid (Other.getid () );                }                 session = localsession;                 return session.getsession ();            }            return null;

else {return super.getsession (create); }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.