Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doGet method

 
sunitha nerevetla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some mock exams, i saw that in the servlet which extends HttpServlet there would be no doGet method but the user invokes get methods either by clicking a link or typing the url in the browser address line. Now the answers would be
1. Throws error since no doGet method defined.
2. Invokes the default doGet method from HttpServlet.

I feel the second answer is right but the answer is other way. Is that right?
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunitha,
In your servlet, you have to either override doGet or doPost.(99.9% of the time!). If you don't override doGet and say, you just click a link, the container will throw an error since you don't have the doGet method defined. Hope this helps.
 
sunitha nerevetla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritu, Thanks for the reply.
I would implement doPost but not doGet, but doesn't the doGet method of HttpServlet will be executed since i haven't overridden the method from HttpServlet i would assume the default implementation of HttpServlet will be executed. I haven't seen in the API, that the doGet from HttpServlet is abstract method.
 
Shourya Kalinga
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunitha,

You are right. doGet from HttpServlet is invoked when you call doGet on a servlet where there is no implementation provided. Both of your answers should be correct, if a slight modification is done

1. Throws error[to the client] since no doGet method defined.[The error may be - HTTP method GET is not supported by this URL]

2. Invokes the default doGet method from HttpServlet.

Hope that helps.

Shourya
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I think both the answers should be correct. If I does not implement the doGet(), the same from the parent will be invoked ( Java Rule). But the default implementation of doGet() method is container dependent and usually throw Server Error ( I Remember 500).

Thanks
 
shankar valiinaykam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunita,

Even I had the similar kind of doubt. Did u get the precise answer for it? Other replies are much more confusing to me by sayin' both are correct. Is this what u r telling , dudes? Default doGet() in HttpServlet is called which throws errors if I dont override doGet()!!!

Oh my gosh!!! One reply says, it is vendor dependent!!! I am under the impression that HttpServlet is a concrete class. How can vendor touch that by any means in this world?

Shankar
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The HttpServlet is abstract class according to the specification provided by Sun. Also the implementation of the J2EE framework is provided by the Vendor and not by the sun.

I am using Tomcat 5. It is giving me following error message.

HTTP Status 405 - HTTP method GET is not supported by this URL

--------------------------------------------------------------------------------

type : Status report

message : HTTP method GET is not supported by this URL

description : The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28


Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic