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 Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HF-pg98 : HttpServlet is an abstract class ?

 
Frederico Werly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im confused,

If "HttpServlet" is an abstract class, why not my concrect sub-class "MyServlet" dont implement many abstract methods from "HttpServlet" ?

"MyServlet" just override doPost and create other method called myBizMethod();

I think that if im inherited an Abstract class, i must to implement many abstract methods from the super abstract class ... im right ?

Thanks
 
Benny Brown
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Frederico.

I guess the answer to this question is similar to the answer regarding HttpServletResponse and -Request interfaces on top of page 107 HF S&J. The Container vendor has already extended/implemented all servlet abstract classes and interfaces and when we use instances of these classes in our code, we are really using objects of these "vendor"-classes behind the scene.

Hope this helps

Regards
Benny
 
Frederico Werly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, in the response and request interfaces i understand, there are objects that was implemented by container and im using the reference to this objects.

But , in HttpServlet abstract class , i inherited direct from this class .. so i need to implement many abstract methods .

Or the container changes my code to put another class that implemts HttpServlet ? if its true ... so i understand , because "MyServlet" is inherited a class that implements the Abstract class HttpServlet .

Thanks for answer.
 
Colin Fletcher
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frederico, I partially understand what you problem is, I think.

For my 'Movie' servlet, I extended the HttpServlet class.. From the class I can call a custom method do perform some activity.

I don't implement any other methods in the Movie class....

 
Frederico Werly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Colin .. thanks for reply .

but .. i know that this code above run without problem , i just want to understand how ou who implements the abstract methods from HttpServlets ?

If HttpServlet is an Abstract class ...so there are many abstract methods in this class right ? so when i inherited one class from HttpServlet i dont implements these methods ... who implements ? do the container changes in my code to refer to another class taht implements HttpServlet ?

i know that it run .. i want to understand how ...

Thanks a lot for replys
 
vishwa venkat
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServlet class doesn't contain any abstract methods.so u don't have to implement all the methods in that class when u derive from HttpServlet.
If u still have problem in unserstanding, please refer
http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
 
siva raju
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frederico,

Even though HttpServlet is an abstract class, it does not have any abstract methods. Hence, you need not implement any.
But the docs say that you must override atleast one method. The container implementation of these methods like doPost, doGet etc will be to show some kind of an error.
If you do not override, say doGet() and issue an url for this servlet, you might get something like
HTTP method GET is not supported by this URL (on Tomcat 5.0)

Hope this clarifies your doubt.

Cheers,
Siva.
 
siva raju
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, didn't see visu's reply. Else, i wouldn't have reinvented the wheel.
 
Frederico Werly
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok !! i did understand ..

My doubt was that the HttpServlet dont has any abstract methods ... so i dont need to implement any .

Only the class is abstract... there are no abstract methods ..

Thanks a lot friends
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic