tags:

views:

34

answers:

2

I've come across a few other questions that describe a similar, but not identical situation, to mine. This question, for instance, shows pretty much the same problem, except that I'm not using portlets - I'm just using boring ol' JSP+JSTL+EL+etc.

I have two application contexts, and I'd like to import a JSP from one to the other. I know how do that:

<c:import context="/" url="/WEB-INF/jsp/foo.jsp"/>

However, I also want to pass a parameter to the imported foo.jsp. But this code:

<c:import context="/" url="/WEB-INF/jsp/foo.jsp">
    <c:param name="someAttr" value="someValue"/>
</c:import>

does not seem to properly send the parameter to foo.jsp; if foo.jsp is something like*

<% System.out.println("foo.jsp sees that someAttr is: "
                      + pageContext.findAttribute("someAttr")); %>

then this gets printed out:

foo.jsp sees that someAttr is: null

whereas I want to see this:

foo.jsp sees that someAttr is: someValue

so, obviously, someAttr can't be found in foo.jsp.

How do I fix this?


*(yes, I know, scriplets==bad, this is just for debugging this one problem)

A: 

Your context must start with a / also. Thus, in your case the code would become like this,

<c:import context="/theOtherContext" url="/WEB-INF/jsp/foo.jsp">
    <c:param name="someAttr" value="someValue"/>
</c:import>

Mind the leading slash in context name. Furthermore, try this instead.

<% System.out.println("foo.jsp sees that someAttr is: "
                      + request.getParameter("someAttr")); %>

As we are setting param not an attribute. For setting pageContext's attribute we have <c:set> but that is not the sub-tag of <c:import>. I hope you are getting my point.

One more thing to point out is, as you are trying to import a JSP page using relative URL from some other external web application in the same server. You must tell the name of the context after the /. In case, you don't want to then you should provide an absolute URL, I believe.

Adeel Ansari
Gah, the context does actually start with a slash - sorry, the second `c:import` should have the same exact context as the first in my question, namely, `context="/"`. Copypasta fail. Fixing now.
Matt Ball
+2  A: 

You're setting it as a request parameter, so you should also be getting it as request parameter.

Since you seem to dislike scriptlets as well, here's an EL solution:

${param.someAttr}

Note that <c:import> doesn't add any extra advantages above <jsp:include> in this particular case. It's useful whenever you want to import files from a different context or an entirely different domain, but this doesn't seem to be the case now. The following should also just have worked:

<jsp:include page="/WEB-INF/jsp/foo.jsp">
    <jsp:param name="someAttr" value="someValue" />
</jsp:include>

This way the included page has access to the same PageContext and HttpServletRequest as the main JSP. This may end up to be more useful.

BalusC
I am actually trying to import JSPs from a different context. I'll try retrieving it with your `${param.someAttr}` suggestion - if that works, is there any way to access `someAttr` as if it were an attribute of the page/request/etc (e.g. like `${someAttr}`)?
Matt Ball
No, it concerns a different request. Session is best what you can get if the contexts share the same session (configureable in server config). Regardless, I'd prefer request parameters to avoid unforeseen clashes among different requests in the same session.
BalusC
Spot on as always. Thanks!
Matt Ball
You're welcome.
BalusC