What is the life cycle of a jsp PageContext object - is it thread safe?

Created and destroyed jsp PageContext objects as part of the http request-response loop, or they are cached and reused between requests.

PageContext has lifecycle methods that offer reuse between requests. those. initialize (), release ().

If they are reused, this can create serious problems with concurrency: if two HTTP requests arrive, requesting the same jsp page, and each request is processed by its own stream, but sets the attributes for the general PageContext object, they will display each other.

Any help appreciated. By the way, I use the servlet container built into Apache Sling.

+4
source share
2 answers

PageContext is only available on a JSP page. If your request was first processed by a servlet and then redirected to a JSP page (using RequestDispatcher.forward), pageContext is available only on this JSP page, but there is no way to access it from the servlet (since there was no page at that time).

In terms of the JSP page, it gets a new pageContext every time it is called. Page contexts can be combined internally, but not shared by multiple JSP pages at the same time.

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

+4
source

Peter is right. PageContext provided for page processing. Consumers should not reference these instances outside this scope, which implicitly means that instances should not be available outside the current stream.

Example JSP processing code from JSP 2.2 Specification :

 public class foo implements Servlet { // ... public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JspFactory factory = JspFactory.getDefaultFactory(); PageContext pageContext = factory.getPageContext( this, request, response, null, // errorPageURL false, // needsSession JspWriter.DEFAULT_BUFFER, true // autoFlush ); // initialize implicit variables for scripting env ... HttpSession session = pageContext.getSession(); JspWriter out = pageContext.getOut(); Object page = this; try { // body of translated JSP here ... } catch (Exception e) { out.clear(); pageContext.handlePageException(e); } finally { out.close(); factory.releasePageContext(pageContext); } } 

How an instance of PageContext is created (from pools or instance creation) is a detail of the container implementation.

+3
source

Source: https://habr.com/ru/post/1399968/


All Articles