Confusion # 1
I understand that HttpServlet class is an abstract class. But when i was going thru the specifications, i couldn't identify which methods of it are abstract.
Also i came accross the following statement in the specs.
A subclass of HttpServlet must override at least one method, usually one of these:
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself
Why is such requirement? From my SCJP exp., if i have an abstract class and i am sub-classing it, then i must implement all the abstract methods otherwise delcare the sub-class also abstract.
For example sake, i can compile my class which extends HttpServlet, even if i don't override doGet method. It means doGet method is not a abstract method.
I am assuming same is the case with rest of the methods too. In such case, why do i need to implement at least one of these methods? what rule is governing this?
Confusion # 2
Assuming doGet method is not an abstract method, HttpServlet class should provide implementation for the same. Is this correct?
Thank you in advacne!
Sekhar -- SCWCD 1.3 scheduled for September 10th!!
Secondly, if you look at the sibling classes of HttpSerlet namely HttpServletRequest and HttpServletResponse etc, all of these are interfaces and we don't implement those, but still we make use of these. The answer lies in the earlier explanations as these too are implemented by Server provider.
The HttpServlet class is abstract, but all of its methods are implemented. There is nothing inherently wrong with this, but as you pointed out a little confusing. Java has no requirement that an abstract class must have one or more abstract methods. It just means that the class cannot be instantiated by the JVM (and enforced by the complier).
The reason why the doXyz methods are implemented is that the spec writers didn't want to force the servlet developer to have to implement all of these: doGet, doPost, doPut, doDelete, and so on. Most of these HTTP methods are of no interest to the casual servlet developer. So... the HttpServlet class implements all of these methods and the typical behavior is to send an 501 (Not Implemented) HTTP status code. Therefore, (as the javadocs state) you must override one or more of these methods to get the behaviour you are trying to achieve. So... the HttpServlet implementations are merely a convience for the servlet developer. Make sense?
doGet method not found
This is where i think i am still confused. If doGet method is already implemenetd, then i am assuming the default implementation wouldn't do anything, so even if i call my servlet without defining doGet, i should get no output, but not the error.
Originally posted by Sekhar Kadiyala:
But as a continuation of this discussion, if the methods are implemented (for example doGet) then, if my servlet doesn't have doGet method and i am trying to access the servlet thru address bar (which will invoke the HTTP method GET), then technically i shouldn't be getting the error as my servlet extends HttpServlet. But it does throw the error saying doGet method not found
OK, there are "compiler errors" and "HTTP errors". In your scenario, you will not get a compiler error (because HttpServlet implements doGet), but you will get an HTTP error when you invoke the servlet from the browser. Is this the behavior that you are seeing? If so, then have I sufficiently explained why this is happening? If not, then please let me know what you see in the browser.