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 overriding both doget() and dopost() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "overriding both doget() and dopost()" Watch "overriding both doget() and dopost()" New topic
Author

overriding both doget() and dopost()

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78

If i override both doget() and dopost() in a servlet,then how is it decided that which method will be called???
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

It depends on the HTTP request.

If you use an HTML form to access your servlet and the method is set to post, then the doPost() method will be called.

<form method="post" action="YourServlet.do">

If you set the form method to get or if you access the servlet directly via a url then the doGet() method will be called.

<form method="get" action="YourServlet.do">

http://mysite..com/YourServlet.do

Also I believe if you don't set the form method then the default is a get request.


OCA7
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Actually, neither will be called as doget() and dopost() are not defined by the Servlet Spec. It's doGet() and doPost().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1105
    
    1

Hi Geet

If are asking your question from Front end (JSP / HTML) perspective then Loius has provided an explanation

But if are asking from server side perspective then answer is -->
once we send a GET/POST request to our server code then doService() method (generic method) decides whether to call doGet() OR doPost() depending upon request type
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overriding both doget() and dopost()