3

I have a JSP that's accessed from an url like

http://localhost/products.jsp

(thus without a query string), while that page includes other JSP's with:

<jsp:include page="product.jsp">
    <jsp:param value="1" name="recordNumber"/>
</jsp:include>

Inside product.jsp there's a call to a Java method that receives the request object:

NavigationUtils.getProductUrl(request)

That method logic is driven by the request parameters.
What I get is that :

  1. request.getQueryString() returns null
  2. request.getParameterMap() has an entry for "recordNumber"

Is this standard behaviour or am I doing something wrong?

I've looked up the docs about HttpServletRequest.getQueryString() and ServletRequest.getParameterMap(), but I can't find that behaviour described nor any reference to a container-dependent handling that may yield different results.

The main issue is that I may break existing code using getParameterMap() instead of getQueryString(), so any advice on that would help.

skaffman
  • 398,947
  • 96
  • 818
  • 769
watery
  • 5,026
  • 9
  • 52
  • 92
  • 1
    I would transform the JSP into a JSP tag file, and pass the parameter as a tag attribute, instead of using an include and passing the parameter as a "fake" request parameter. That would have the additional advantage of being able to pass it as a proper Integer, rather than a String. – JB Nizet Mar 27 '15 at 11:19
  • @JBNizet My code rewriting span is narrower than that, in fact I'd rather prefer not to touch any JSP, I'm here because the existing code (which is deep inside the webapp logic) prevents my new implementation from working as expected, but thank you for the suggestion. – watery Mar 27 '15 at 11:42

1 Answers1

2

The query string is nothing more than a mechanism to encode parameters in a request, but it's not the only one. Typically, that's used when a browser sends a GET request to the server. Another mechanism would be in the body of a form-encoded POST request.

In your case, the JSP wants to include the results of another JSP, which all happens server-side. The servlet container can pass parameters from one JSP to the other without having to encode the parameter on the query string (which would be unnecessarily inefficient).

Using getParameter or getParameterMap is the more general solution. Using getQueryString only really makes sense in specific circumstances when that's eexplicitly what you need to look art.

skaffman
  • 398,947
  • 96
  • 818
  • 769