5

I have exactly the same basic question about accessing jsp:param values as this poster; following his example exactly does not work for me. The parameters passed in via jsp:include don't seem to show up in the included file. Is there something peculiar about my setup?

Caller:

<div>
    <jsp:include page="../../../common/callee.jsp">
        <jsp:param name="justinVar" value="primary" />
    </jsp:include>      
</div>

callee.jsp:

<i>method 1: [</i><b><%= request.getParameter("justinVar") %></b><i>]</i>
<p/>
<i>method 2: [</i><b>${param.justinVar}</b><i>]</i>
<p/>
<i>method 3: [</i><b>${justinVar}</b><i>]</i>
<p/>

Final output:

method 1: [null]

method 2: []

method 3: [] 

Update: The following workaround does work, it seems wrong, but perhaps the fact that it works reveals something that is not working.

<c:set var="justinVar" value="justinVARisHere" scope="request" />
<jsp:include page="../../../common/callee.jsp" />
Community
  • 1
  • 1
Justin
  • 4,437
  • 6
  • 32
  • 52
  • 1
    The first and second should work (of which the first is an old fashioned and poor approach btw). The third is wrong, it only scans for page/request/session/servletcontext attributes, not request params. Are you sure you're running the code you think you're running? Aren't you in *real* code dynamically filling the `value` attribute of `jsp:param` or something? – BalusC Apr 25 '11 at 16:28
  • Well, my goal is to create a parametirized editor for a complex bean. I want 1-2 of these editors to show on a page and bind to a sub-object of the page bean, the parameter is supposed to be the object path name of the sub object. This example is 'real code' since I can't get anything to work and went back to basics. – Justin Apr 25 '11 at 16:50
  • What JSP/Servlet container make/version are you using? To what Servlet version is your `web.xml` been declared? Do you have any servlet container specific libraries in webapp's `WEB-INF/lib`? EL works out the box since Servlet 2.4/JSP 2.0 only, but you would otherwise have seen unparsed EL code in the HTML output which isn't the case here. Although I am a bit shooting in the dark here, it also doesn't explain why the old way also doesn't give anything. You must be not running the code you think you're running. Cleanup the environment, cleanup server work folder, recreate test files, etc. – BalusC Apr 25 '11 at 16:53
  • I am using jetty 7.1.6, servletAPI 1.2, tomcat's jasper (6.0.29); all from maven. The app is spring MVC (spring 3.0.3). I'm running it out of my IDE using a custom launcher. Everything else seems to work, just not jsp:include. My web.xml has – Justin Apr 25 '11 at 16:59
  • 2
    Try a real servletcontainer instead of Jetty, such as Tomcat or Glassfish. I've seen too much oddities with regard to Jetty before. I won't be surprised if this is another one again. – BalusC Apr 26 '11 at 19:29
  • The path, gone down, is too far to retrace at the moment. – Justin Apr 26 '11 at 19:37

5 Answers5

3

To nail down the problem, try to debug/explore the entire map by printing ${param} in EL or HttpServletRequest#getParameterMap() in Java code. It must give insights about what the map really contains.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My servlet uses a custom wrapper around HttpServletRequest to do REST full parameter parsing. This was interfering with Jasper's method of manifesting a jsp:include. – Justin Apr 26 '11 at 21:13
1

Right. I have had a similar problem that for the last hour or so and I have found a solution for my case.

I had index.jsp and tried to include news.jspf in it with text from a jsp param in the index.jsp It didn't work. It just showed the news.jspf EL as normal text.

I changed the included file name extension from .jspf to .jsp and it fixed the problem.

I'm using Eclipse, which may or may not be a factor.

Good luck

grooble
  • 11
  • 1
0

This work in my Liferay portlet

ParamUtil.getString(PortalUtil.getOriginalServletRequest(request), "justinVar")
trungnnh
  • 263
  • 2
  • 7
0

This issue can happen if the request is wrapped (i.e. HttpServletRequestWrapper), likely via a filter. See: jsp:param no longer sets parameters when original request is wrapped with a HttpServletRequestWrapper

Sam
  • 6,240
  • 4
  • 42
  • 53
0

I've traced down the generated java source and it looks reasonable. Either one of two things are happening: org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode is messing up the varible name, or the classloader used resolves to JspRuntimeLibrary to some other instance than the one driving the web app.

// caller.jsp ....
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, 
  "callee.jsp" + (("callee.jsp").indexOf('?')>0? '&': '?') + 
  org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("justinVar",
  request.getCharacterEncoding())+ "=" +
  org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("primary", 
  request.getCharacterEncoding()), out, true
);

Looking inside Jasper's runtime, we find this gem. Its not clear that this is the problem, however the page being rendered is jsp:include from defaultParent.jsp, although jsp:params passed into the jsp:include from defaultParent don't seem to show up either.

  958           // FIXME - It is tempting to use request.getRequestDispatcher() to
  959           // resolve a relative path directly, but Catalina currently does not
  960           // take into account whether the caller is inside a RequestDispatcher
  961           // include or not.  Whether Catalina *should* take that into account
  962           // is a spec issue currently under review.  In the mean time,
  963           // replicate Jasper's previous behavior
Justin
  • 4,437
  • 6
  • 32
  • 52
  • 1
    Those strings doesn't contain any special characters which are illegal in URLs. – BalusC Apr 26 '11 at 19:28
  • Looking at the source to URLEncode, the outputted stuff will be ISO-8859-1, which _shouldnt_ cause any problems. I suspect there may be two instances of JspRuntimeLibrary (parented by two classloaders), one probably illegitimate. – Justin Apr 26 '11 at 19:40
  • `URLEncoder` shouldn't swallow the characters. The worst thing which can happen is that you get malformed characters. – BalusC Apr 26 '11 at 19:46
  • I was thinking that an EL expression in the include might not be able to find the parameter since its name will be expressed in a different encoding than that of callee.jsp. – Justin Apr 26 '11 at 19:50
  • 1
    Ah right. But for ASCII chars this shouldn't happen. Otherwise explore the entire map by printing `${param}` in EL or debugging `HttpServletRequest#getParameterMap()`. – BalusC Apr 26 '11 at 19:52
  • @BalusC pure genius, post the above this as an answer and will accept it! I seem to be using a customized HttpServletRequestWrapper (ParameterizedPathServletRequest) which takes REST URL parameters and puts them in the request. It over-rides getParameterMap() to return its own parsed URL parsed version. Jasper's method of appending the `URL?"jsp params"` does not seem to work since nothing triggers the custom REST parsing. – Justin Apr 26 '11 at 20:27