views:

2455

answers:

4

When you get a doGet or doPost call in a servlet you can use getparameterxxx() to get either the query string or the post data in one easy place. If the call was a GET, you get data from the url/query string. If the call was a POST, you get the post data all parsed out for you.

Except as it turns out, if you don't put an 'action' attribute in your form call. If you specify a fully qualified or partially qualified url for the action param everything works great, if you don't, the browser will call the same url as it did on the previous page submit, and if there happens to be query string data there, you'll get that as well as POST data, and there's no way to tell them apart.

Or is there? I'm looking through the request object, I see where the post data comes from, I'm just trying to figure out where the GET data comes from, so I can erase the GET data on a post call and erase the post data on a GET call before it parses it out if possible.

Any idea what the safe way to do this is?

And lemme guess: you never tried to not put an action field in a form tag. :-)

+1  A: 

The HttpServletRequest.getParameterxxx() methods don't distinguish between GET and POST parameters. If you really need to distinguish between them, you'll need to parse them manually using getQueryString() for the GET parameters and getInputStream()/getReader() for the POST data.

Hilton Campbell
+2  A: 

You're right, I never tried not to put an action field in a form tag ;-) and I wouldn't, because of exactly what you're talking about. (Also, I think it's not valid HTML)

I don't know of any "clean" way to distinguish between GET and POST parameters, but you can access the raw query string using the getQueryString() method of HttpServletRequest, and you can access the raw POST data using the getInputStream() method of ServletRequest. (I'm looking at the Tomcat API docs specifically here, although I think those are both part of the standard Servlet API) Then you could parse the POST data and GET data separately if you want. They will (or should normally) both be formatted the same way, i.e.

name1=value1&name2=value2&...

though possibly with the ampersands replaced by semicolons (which you can technically do in HTTP/1.1, I didn't know that until recently)

David Zaslavsky
Yeah, I think this is the best you can do. The moral of the story is include the action and method in your form...
Neil Coffey
Easy to say when you didn't inherit large system with lots of forms with no actions. :-) But I see your point. Thanks.
stu
A: 

I would write a ServletFilter and decorate the request object to clean things up a bit (using what Hilton suggested above). This is the classic decorator pattern in an intercepting filter.

kipz
+2  A: 

In HTML, action is REQUIRED, so I guess the behavior will vary among clients.

MatthieuP
Yet this still occurs when action is present, if the action URL includes a query string.
Hilton Campbell
I've never see that, but I have tried the no action thing in various browsers and they all behave the same way.It probably depends on how qualified the action parameter is...
stu