Get link to JspContext / PageContext from servlet

Does anyone know how to get a JspContext link from a servlet?

I have a servlet that forwards Jsp, and I would like to set some PageContext variables from the servlet so that they are ready for consumption in Jsp.

+4
source share
2 answers

Let me see if I understand: you want to call the JSP from the servlet and make some variables (which are under the control of the servlet) available to the JSP. Right?

Then forget about the PageContext , it is just specific to JSP pages and cannot be accessed from the servlet. Any attribute specified in the context of a request, session, or servlet will be available in the JSP. PageContext is a region wider than the previous one, and it has a findAttribute method findAttribute , when called, will look for an attribute with the given name inside the context context of the page, request, session, or servlet (in that order).

So, you only need to set these variables as attributes in one of these areas, I would suggest using request one ( HttpServletRequest.setAttribute("foo", "fooValue") ) and then use it in your JSP using a value expression ( ${foo} ).

+6
source

You must use the request area. The pageContext context is obtained by the implementation-dependent subclass of JspFactory in the JSP service method. In Tomcat for example

 public void _jspService( ... pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true); 

So pageContext does not exist before the request is sent to the JSP.

+1
source

All Articles