jspthread-safetycustom-tagsslingservlet-container

What is the lifecycle of a jsp PageContext object - is it threadsafe?


Are jsp PageContext objects created and destroyed as part of the http request-response cycle or are they cached and reused between requests.

PageContext has life-cycle methods that suggest reuse between requests. ie initialize(), release().

If they are reused this could pose serious concurrency problems: if two http requests arrive, requesting the same jsp page, and each request is processed by its own thread, but sets attributes on a shared PageContext object, they will render each others' content.

Any help appreciated. By the way I am using the servlet container embedded in Apache Sling.


Solution

  • PageContext are only available from your JSP page. If your request was first handled by the servlet, and then forwarded to JSP page (using RequestDispatcher.forward), pageContext is only available on this JSP page, but there is no way to access it from the servlet (because there was no pageContext yet at that time).

    From JSP page point of view, it is getting new pageContext every time it is called. Page contexts may be pooled internally, but not shared by multiple JSP pages at the same time.

    initialize and release methods have this comment: "This method should not be used by page or tag library authors." Just forget about them.