Re: Servlets and form posts/querystring

J

John C. Bollinger

Miguel said:
What if I have a form like this:

<form action="blah?displaymode=update&section=3" method="post">
...

I know that its probably not good practice, but that's how my boss wants it.

I wondered whether you were doing something like this. Is there any
real need to draw a distinction? The ServletRequest parameter access
methods will provide access to a combination of the query string and
message body parameters. Would it actually be a problem if parameters
that were usually provided in the form data were provided in the query
string instead, or vise-versa? If your application depends on such
distinctions then it is likely quite brittle.

If you really do need to perform this sort of check then you can use
HttpServletRequest.getQueryString() and parse the result yourself.
Although I hesitate to even mention it, there is also the deprecated
javax.servlet.http.HttpUtils class that provides methods you might be
able to use -- but it is deprecated, so I don't recommend it.


John Bollinger
(e-mail address removed)
 
M

Miguel De Anda

John C. Bollinger said:
I wondered whether you were doing something like this. Is there any
real need to draw a distinction? The ServletRequest parameter access
methods will provide access to a combination of the query string and
message body parameters. Would it actually be a problem if parameters
that were usually provided in the form data were provided in the query
string instead, or vise-versa? If your application depends on such
distinctions then it is likely quite brittle.

If you really do need to perform this sort of check then you can use
HttpServletRequest.getQueryString() and parse the result yourself.
Although I hesitate to even mention it, there is also the deprecated
javax.servlet.http.HttpUtils class that provides methods you might be
able to use -- but it is deprecated, so I don't recommend it.


John Bollinger
(e-mail address removed)

I see. Thanks a lot. I did notice though, that the querystring parameters
seem to override the form post. For example, if a form field is named id and
value of "1" and the querystring somehow has ?id=2, then it seems like I get
id=2.
 
J

John C. Bollinger

Miguel said:
I see. Thanks a lot. I did notice though, that the querystring parameters
seem to override the form post. For example, if a form field is named id and
value of "1" and the querystring somehow has ?id=2, then it seems like I get
id=2.

I haven't tested this case specifically, but it is likely that you are
ending up with a multi-valued parameter. From the API docs for
ServletRequest.getParameter:

"Returns the value of a request parameter as a String, or null if the
parameter does not exist. Request parameters are extra information sent
with the request. For HTTP servlets, parameters are contained in the
query string or posted form data.

You should only use this method when you are sure the parameter has only
one value. If the parameter might have more than one value, use
getParameterValues(java.lang.String).

If you use this method with a multivalued parameter, the value returned
is equal to the first value in the array returned by getParameterValues."

I believe the Servlet specification has more details on how parameters
are handled in HttpServlets, and I recommend that you get that and read
it if you have not already. It is available as a free download from
Sun. If that is too heavy a read then at least do carefully read the
relevant API docs.


John Bollinger
(e-mail address removed)
 
M

Miguel De Anda

John C. Bollinger said:
I haven't tested this case specifically, but it is likely that you are
ending up with a multi-valued parameter. From the API docs for
ServletRequest.getParameter:

"Returns the value of a request parameter as a String, or null if the
parameter does not exist. Request parameters are extra information sent
with the request. For HTTP servlets, parameters are contained in the
query string or posted form data.

You should only use this method when you are sure the parameter has only
one value. If the parameter might have more than one value, use
getParameterValues(java.lang.String).

If you use this method with a multivalued parameter, the value returned
is equal to the first value in the array returned by getParameterValues."

I believe the Servlet specification has more details on how parameters
are handled in HttpServlets, and I recommend that you get that and read
it if you have not already. It is available as a free download from
Sun. If that is too heavy a read then at least do carefully read the
relevant API docs.


John Bollinger
(e-mail address removed)


Oh, so the getParameterValues() is good for like checkboxes or radio buttons
with the same name right? (That actually answers my next question.)
 
J

John C. Bollinger

Miguel said:
Oh, so the getParameterValues() is good for like checkboxes or radio buttons
with the same name right? (That actually answers my next question.)

I'd have to check the HTML spec for the expected behavior in those
cases. If I recall correctly then for checkboxes you would get one
parameter (if any of the like-named boxes was checked) with one or more
empty values. For radio buttons I think the whole point is that you
only ever get the value associated with the selected option.


John Bollinger
(e-mail address removed)
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top