• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it a POST or GET request?

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
}

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Or are you expecting other sites, not defined by you, to be submitting to your servlets?



No.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic