JSP getQueryString() and getParameterMap() returning different parameters - java

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 :
request.getQueryString() returns null
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.

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.

Related

How to get parameters from the URL in Liferay

I have a portlet developed in Liferay, in which I want to get query parameter value from URL.
I tried this way but get "null" value from Query parameter:
HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
HttpServletRequest httpOrigReq = PortalUtil.getOriginalServletRequest(httpReq);
String myValue = httpOrigReq.getParameter("idProcessOrigin");
Any advice would be greatly appreciated!
The code you mention in your question should work, however, it's ignoring the peculiarities of a portlet environment. Typically, in a portlet, you'd rather "decorate" the names of parameters with <portlet:namespace/> (or whatever the equivalent in your UI library of choice is to this JSP tag). Instead of submitting a parameter "idProcessOrigin", you'd submit "<portlet:namespace/>idProcessOrigin" (of course, with properly replaced namespace, e.g. rather SOME_RANDOM_STUFF_idProcessOrigin)
If you don't want this, you can also declare the property com.liferay.portlet.requires-namespaced-parameters=<boolean> in your portlet-#Component's property list (as carried over from liferay-portlet.xml)
For the standard way of obtaining the parameters from a portlet request, you don't need to go through the HttpServletRequest at all - just use the PortletRequest's getParameter method. The result of this method depends, however, on properly decorated parameter names (or the deactivated option mentioned above). Note: When you call request.getParameter("idProcessOrigin"), you don't need the decoration any more, provided that request is a PortletRequest, not an HttpServletRequest.

Reading URL parameters when POST request with data arrives in Java Servlet

In Java servlets you read a JSON from a POST request e.g. via
new JSONObject(toString(httpRequest.getInputStream()))
Now additionally to the JSON I would like to specify parameters in the URL, they can be read via:
httpRequest.getParameterMap().get("someURLParam")
All is working (I'm using AJAX post requests and jetty for server side)
BUT
I'm concerned and confused if and when these two methods influence each other as the javadocs from javax.​servlet.​ServletRequest.getParamter(String) says:
If the parameter data was sent in the request body, such as occurs
with an HTTP POST request, then reading the body directly via
ServletRequest.getInputStream or ServletRequest.getReader can
interfere with the execution of this method.
What does it mean in my case? Or do they only interfere if content type is x-www-form-urlencoded? Or only if using getParameter and the method getParameterMap is fine?
If you are only using getParameter/getParameterMap, you will be fine. This is because, behind the scenes, those methods may call getInputStream. The spec says MAY because it's up to the implementation, so the behavior may vary from one container to another.
If your content isn't form encoded, or you are processing a GET request, etc., getParameter/getParameterMap only needs to get the parameters from the query string, so it makes sense that Jetty wouldn't read the body in those cases.

How servlet handle multiple request from same jsp page

Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.

Why does getAttribute() in HttpServletRequest works for GET method but not for POST method?

I'was debugging a client-side AJAX problem that is making request to servlet. But the bug turned out to be at server side. You can refer to my original question here. From the discussion with more experienced people, I found out that servlet is using request.getAttribute() method to retrieve parameters from the request instead of getParameter(). So I thought to open a new question to clear my doubt.
Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?
You should always use getParameter, when attribute come from GET or POST method. And use getAttribute when request is forwarded from another servlet/jsp. Such as :
ServletA:
request.setAttribute("test","value")
request.getRequestDispatcher("/ServletB").forward(request, response)
ServletB:
request.getAttribute("test") <-- you can get test attribute by using getAttribute
Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?
Complete nonsense. You're apparently working on an existing project which has a lot of other existing servlets and filters. My cents that there's another filter in the request-response chain which maps request parameters to request attribtues for some unobvious reason.
Please create a blank playground project and create a playground servlet to familarize yourself better with servlets without all that noise in existing projects.
See also:
Our Servlets wiki page

passing values between servlet and java file

I've one spring controller which is setting some values to request and shows a jsp page. For the view part we use tiles. The result page has 3 parts, header , content and footer jsp's.
This header jsp use a java file and i want to access the attributes created by the first spring controller from this file. Is there any way to do that without using session?
When I tried request.getAttribute, it gives null. I think it's because it's not an immediate file after the request values setting.
As long as everything runs in the same request and the controller code is executed before the view part, setAttribute() should work. To debug issues like that, use a Filter which dumps the request URL and attributes to the console or the log.
If those calls are in different requests, you have two options: The session and a Spring bean (use a session bean or your own implementation). I prefer beans since they are type safe and they allow me to separate my code from the Servlet API which is complex to test.
You'll really need to put some code to get a code answer but unless you're using JSP scriptlets I'm guessing this is a Java bean that you're using in the header. This of course cannot access the request (hence the session) nor should it really. What you probably want to do is convert it to a tag library if you want it to have access to the request/session.

Categories