File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes doPost and doGet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doPost and doGet" Watch "doPost and doGet" New topic

doPost and doGet

lakshmi v.samy
Ranch Hand

Joined: Feb 02, 2006
Posts: 30
Hi friends
I have one doubt

In one html
<form action="myservlet" method=Get>

In another html
<form action="myservlet" method=Post>

please help me what method(doGet,doPost or service) I can override in servlet-(myservlet is extending HttpServlet)to do the actions.I want to have only one servlet(myservlet) for both the html files.
Thanks in advance.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

You can override doPost(for POST) and doGet(for GET) in a single Servlet class.

Arunprasath Rajmohan

Joined: Jan 27, 2006
Posts: 4
hello lakshmi

If u use post in ur html form then u have to use doPost in servlet.
If u use get in ur html form then u have to use doPost or doGet in servlet.

But what u should not is if u use post in html and if u use doGet in servlet it will give a nasty error

POST- doPost()
GET- doGet(),doPost()

the below combination will rise an error:
POST doGet()
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Usually you want to use POST method for form submission. By default, form method is GET.

Yup, usually I overrided both and one of the method just delegate the call to another.
[ February 07, 2006: Message edited by: Eddy Lee Sin Ti ]

SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="" target="_blank" rel="nofollow"></a>
Ben Souther

Joined: Dec 11, 2004
Posts: 13410


Many of the people using Javaranch do not speak English as a first language.
Some rely on lanugage translation software to interpret the text on these pages. We ask that you do not use abbreviations when typing here. Use "you" instead of "u" and "you are" or "you're" instead of "ur".
Such abbreviations confound this software making it impossible for these people to read your answers.

Thank you for your understanding,
-The Bartender.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

POST- doPost()
GET- doGet(),doPost()

How can a doPost() method get called for HTTP GET.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think it's pretty common to override doGet and doPost and have both call a common method like doMyThing. Unfortunately it says you're not thinking about the meaning of GET or POST (or PUT or DELETE) and designing with the meaning in mind.

For some neat ideas on this read up on Restful Architecture here or on the Wikipedia.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: doPost and doGet
It's not a secret anymore!