Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doGet() and doPost()!!!

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
T Rai
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic