Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP, GET and POST parameters

Tags:

java

jsp

servlets

I am required to do some small tasks with JSP; being very new to JSP I was wondering if there was any possibility to get only GET or only POST parameters from the HTTP request.

I have seen ServletRequest.getParameter (and alikes) but it seems that those methods get both GET and POST parameters. Is there a way to get only one of them, without parsing the URL or the request body myself? And if not, is there any precedence rule which values overwrite which (like POST parameters always overwriting GET parameters)?

like image 409
poke Avatar asked Nov 14 '10 15:11

poke


1 Answers

Generally, requests should better be handled in servlets. They have doGet(request, response) and doPost(request, response) methods, to differentiate the two.

If you really insist on doing it in a JSP, you can differentiate the methods using request.getMethod(). It would return GET or POST.

Since this is homework, I guess the point is to learn how to use servlets and their doX methods, so do it that way.

Update: You can get the query string (request.getQueryString()), which is only the get parameters, and parse it, but I wouldn't say that's a common and good practice.

like image 108
Bozho Avatar answered Oct 08 '22 02:10

Bozho