I’m working on a jsp(test.jsp) in a web-app with a servlet context of /hello. I am doing the following:
<c:import url = "/other/altTest.jsp" context="/" />
It appears that request attributes available to test.jsp are unavailable to the imported jsp from the c:import (altTest.jsp), I suspect due to the context parameter. Could someone please explain the reason behind this as well as suggest an alternative method of importing that jsp where I could access request attributes?
The
c:importfires under the covers a new and independent HTTP request. Even if the file was in the same context, it wouldn’t have access to the same request as the parent JSP was opened with.Your best bet is to pass the information as request parameter or maybe store as session attribute and then let the imported JSP remove it from the session. Still, this require that the different contexts share the same session –this is configureable at servletcontainer level, in for example Tomcat, set the
emptySessionPathattribute totrueincontext.xml.An alternative is to use a servlet instead wherein you can do
ServletContext#getContext()to obtain the other servlet context (only ifcrossContextattribute is set totrueincontext.xml) and then obtain theRequestDispatcherand finally invoke theforward()on it. This isn’t possible by JSP tags.