File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is it a POST or GET request? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is it a POST or GET request?" Watch "Is it a POST or GET request?" New topic
Author

Is it a POST or GET request?

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
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?
}



BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

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

Joined: Oct 15, 2002
Posts: 189
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 ]

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
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

Joined: Jan 10, 2002
Posts: 61774
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
Or are you expecting other sites, not defined by you, to be submitting to your servlets?


No.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

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

Joined: Sep 20, 2001
Posts: 671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it a POST or GET request?