This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes doGet() and doPost()!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "doGet() and doPost()!!!" Watch "doGet() and doPost()!!!" New topic
Author

doGet() and doPost()!!!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
hi folks,
i have the following code:

when i execute this servlet, why the doGet()method is always invoked even though this method is not explicitly called? I know when a form is submitted, a method attribute value is set either GET or POST that makes either doGet() or doPost() being called accordingly. but when GET or POST is not explicitly called like i execute a above servlet, what makes the doGet() method of the servlet invoked and why not doPost()?
please clarify me this mechanism.
And Also, why does setContentType("text/html") have to be present? can't we do without it? I tried running above servlet without it, bud didn't see any problem?When does it cause a problem without it?
thanks.
[ September 09, 2003: Message edited by: Namaste Sathi ]
[ September 13, 2003: Message edited by: Jim Yingst ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
doGet is the default method called when either a method is not specified within a form tag, the servlet is accesed by a link, or by typing the url in the address bar.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
the default contentType for a response is text/html.
This is not necessary and does not affect your code if you do not have it but it will be helpful while reviewing your code if you set different response contentTypes through out your application e.g. text/xml, etc.
Thank you,
Maneesh


Sun Certified Java Programmer 2 (1.4)<br />Sun Certified Web Component Developer
T Rai
Greenhorn

Joined: Apr 16, 2002
Posts: 20
Originally posted by maneesh subherwal:
the default contentType for a response is text/html.
This is not necessary and does not affect your code if you do not have it but it will be helpful while reviewing your code if you set different response contentTypes through out your application e.g. text/xml, etc.
Thank you,
Maneesh

i am not sure how setContentType will be helpful while reviewing code if it does not matter setContentType is set or not. can you please clarify this in a bit detail?
thanks.
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
I believe that setting the content-type is only unnecessary in the case where html is expected to be displayed since the default is text/html. it is necessary in other cases, such as image/gif, etc.
The setContentType method sets the MIME type (Multipurpose Internet Mail Extensions) of the response. Many MIME types currently exist other than the ones mentioned here.
While reviewing code and for developers who are unsure of what the default content type is, it is good to explicitly set it in the code even if the response is in text/html.
I hope this helps...
Thank you,
Maneesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doGet() and doPost()!!!
 
Similar Threads
Strange problem - request forwarding
File not found error
Why is javascript function called even before onClick?
HTTP POST-Method not supported by this URL.
use doGet & doPost Method