This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it a POST or GET request?

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the servlet how do I know a request is from POST (form submission request) or GET (URL request)?

Example servlet:


public void doGet(HttpServletRequest request, HttpServletResponse response) {
process(request);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) {
process(request);
}

public void processt(HttpServletRequest request) {
// Here how do I know the request is from doPost or doGet?
}

 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, HttpServletRequest has a getMethod() method. But in most designs the programmer should already know whether the servlet is meant to handle GET or POST requests, and just code a doGet() or doPost() method.
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are collapsing doGet() and doPost() into a single process() method, then to all appearances you don't care if it is GET or POST. If the logic is to be different based on the method, then it is usually implemented in the appropriate method handlers. as Paul has already pointed out.

But perhaps the difference in logic is so trivial that you really do want a single service() method. As Bear Bibeault reminded me in a different thread, you also have HttpServletRequest.getMethod() to tell you which HTTP verb was used. If this is the case though, you may want to re-think your design.

Lastly, a service() method traditionally takes the HttpServletResponse object as a second argument -- I think that's the way you see it in the Netbeans HttpServlet templates.
[ February 07, 2007: Message edited by: Philip Shanks ]
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I realized that what I really want to do is to distinguish if a request is from URL (in browser address or a link from a web page) or from subsequent form submission (when user clicks a button to submit the form). Since form submission can also be GET, use of Request.getMethod may not accomplish what I want to do.

I may code a hidden field in form to tell servlet what request is a form request.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are declaring the form elements you have the control to ensure that they are all POST requests. Or are you expecting other sites, not defined by you, to be submitting to your servlets?
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or are you expecting other sites, not defined by you, to be submitting to your servlets?


No.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then all you have to do is to put method="post" on all your forms and you won't need any other goo such as hidden elements.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Sometimes I change method=POST to GET so that I can see how form values are sent to server in the URL address field.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic