Issue with request.getParameter function in Java Servlet

P

paramsethi

Hi All,

I am facing this weird problem in my servlet.

If I print the request.getQueryString() value, I get "action=getall"
but when I do request.getParameter("action"), it prints NULL value.

Any pointers to the reason which could be causing this issue will be
great.

Thanks
Param
 
J

John B. Matthews

paramsethi said:
I am facing this weird problem in my servlet.

If I print the request.getQueryString() value, I get "action=getall"
but when I do request.getParameter("action"), it prints NULL value.

Any pointers to the reason which could be causing this issue will be
great.

What form action method are you using: GET or POST? The method
getQueryString() "returns null if the URL does not have a query string."

<http://java.sun.com/webservices/doc...http/HttpServletRequest.html#getQueryString()>
<http://www.w3.org/TR/html401/interact/forms.html#submit-format>
 
P

paramsethi

What form action method are you using: GET or POST? The method
getQueryString() "returns null if the URL does not have a query string."

<http://java.sun.com/webservices/docs/1.6/api/javax/servlet/http/HttpS...()>
<http://www.w3.org/TR/html401/interact/forms.html#submit-format>

Hi John,

Thanks for the reply. The problem here is opposite :(.

I see the data in request.getQueryString() method. But when I try to
get the same parameter using request.getParameter("action") it returns
me a null value.

Its happening intermittently and not always. The only change I have
done in my App is to use Apache mod_rewrite module to do URL rewriting
instead of using urlrewritefilter-2.6.0.jar which handles URL
rewriting in java.
 
M

Mike Schilling

paramsethi said:
Hi All,

I am facing this weird problem in my servlet.

If I print the request.getQueryString() value, I get "action=getall"
but when I do request.getParameter("action"), it prints NULL value.

Any pointers to the reason which could be causing this issue will be
great.

Does the HTTP request contain a body? If so, it's possible that
getParameter() is looking for the value in the body rather than in the URL.
If not, I have no idea.
 
J

John B. Matthews

[Please do not quote signature lines.]
Hi John,

Thanks for the reply. The problem here is opposite :(.

I see the data in request.getQueryString() method. But when I try to
get the same parameter using request.getParameter("action") it
returns me a null value.

Its happening intermittently and not always. The only change I have
done in my App is to use Apache mod_rewrite module to do URL
rewriting instead of using urlrewritefilter-2.6.0.jar which handles
URL rewriting in java.

OK, getParameter() returns null; how about "If the parameter data was
sent in the request body, such as occurs with an HTTP POST request, then
reading the body directly via getInputStream() or getReader() can
interfere with the execution of this method."

I'm using tomcat 6 and this example and diff:

<http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/webapps/examples/
WEB-INF/classes/RequestParamExample.java>
 
P

paramsethi

[Please do not quote signature lines.]
Thanks for the reply. The problem here is opposite :(.
I see the data in request.getQueryString() method. But when I try to
get the same parameter using request.getParameter("action") it
returns me a null value.
Its happening intermittently and not always. The only change I have
done in my App is to use Apache mod_rewrite module to do URL
rewriting instead of using urlrewritefilter-2.6.0.jar which handles
URL rewriting in java.

OK, getParameter() returns null; how about "If the parameter data was
sent in the request body, such as occurs with an HTTP POST request, then
reading the body directly via getInputStream() or getReader() can
interfere with the execution of this method."

I'm using tomcat 6 and this example and diff:

<http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/webapps/examples/
WEB-INF/classes/RequestParamExample.java>

 <  out.println(rb.getString("requestparams.params-in-req") + "<br>");
 >  out.println(rb.getString("requestparams.params-in-req")
 >    + "&nbsp;" + request.getQueryString() + "<br>");

 <  out.println("method=POST>");
 >  out.println("method=GET>");

Hi John,

I reverted back my changes to Tuckey URL rewrite and everything seems
to be working fine.

The only reason that I could suspect here is, In the mod_rewrite.conf
I am using the below mentioned rule:

RewriteRule /test/([^/?]+)[?]*(.*) /TestServlet?action=$1 [QSA,L,PT]

My request is coming as a POST and action parameter is appended into
URL which means it will be a GET. request.getParameter intermittently
tries to read from request body as the request type is POST but the
action parameter is present in the url.

I am not sure if this is actually the reason, because then it should
fail for every request but it's failing randomly. Some times all the
URLs are successful. :(
 
P

paramsethi

<2965be97-e388-4b69-8400-e1c7d0c68...@j21g2000yqh.googlegroups.com>,
[Please do not quote signature lines.]
Hi John,
Thanks for the reply. The problem here is opposite :(.
I see the data in request.getQueryString() method. But when I try to
get the same parameter using request.getParameter("action") it
returns me a null value.
Its happening intermittently and not always. The only change I have
done in my App is to use Apache mod_rewrite module to do URL
rewriting instead of using urlrewritefilter-2.6.0.jar which handles
URL rewriting in java.
OK, getParameter() returns null; how about "If the parameter data was
sent in the request body, such as occurs with an HTTP POST request, then
reading the body directly via getInputStream() or getReader() can
interfere with the execution of this method."
I'm using tomcat 6 and this example and diff:

 <  out.println(rb.getString("requestparams.params-in-req") + "<br>");
 >  out.println(rb.getString("requestparams.params-in-req")
 >    + "&nbsp;" + request.getQueryString() + "<br>");
 <  out.println("method=POST>");
 >  out.println("method=GET>");

Hi John,

I reverted back my changes to Tuckey URL rewrite and everything seems
to be working fine.

The only reason that I could suspect here is, In the mod_rewrite.conf
I am using the below mentioned rule:

RewriteRule /test/([^/?]+)[?]*(.*) /TestServlet?action=$1 [QSA,L,PT]

My request is coming as a POST and action parameter is appended into
URL which means it will be a GET. request.getParameter intermittently
tries to read from request body as the request type is POST but the
action parameter is present in the url.

I am not sure if this is actually the reason, because then it should
fail for every request but it's failing randomly. Some times all the
URLs are successful. :(

Finally I came back to mod_rewrite again, as I wanted to implement the
same in my app.

I tried putting a logic that If request.getParameter("action") comes
as null/blank (since I know it will be coming always as per the rules
given in mod_rewrite.conf file), parse the request.getQueryString()
method and get the value for action.

When I restarted my application with the above fix, I noticed that
none of the other required parameters which were sent using POST
method were present in the request.

Looks like some where the original request is getting lost and server
is creating a new request object. The intermittent failure of
different URLs is still un explainable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top