obtain reference to JspContext/PageContext from a servlet
jakarta-ee, java, jsp, servlets
Solution
Let me see if I understood: you want to invoke a JSP from a servlet and make some variables (which are under the control of the servlet) available to the JSP. Right?
Then forget about the `PageContext`, it's just specific to JSP pages and it can't be accessed from a servlet. Any attribute you set in the request, session or servlet context will be available in the JSP. The `PageContext` is a scope wider than the previous ones and it comes with a `findAttribute` method that, when invoked, will look for an attribute with given name inside the page's context, request, session or servlet context (in that order).
So, the only thing you need is to set those variables as attributes in one of those scopes, I would suggest to use the `request` one (`HttpServletRequest.setAttribute("foo", "fooValue")`) and then use it in your JSP using a value expression (`${foo}`).
Problem
Does anyone know a way to get a JspContext reference from a servlet? I have a servlet that forwards to a Jsp and I'd like to set some PageContext variables from within the servlet so they're ready for consumption in the Jsp.