File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How use OOPs in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How use OOPs in Servlet" Watch "How use OOPs in Servlet" New topic

How use OOPs in Servlet

kamlesh tayade
Ranch Hand

Joined: Sep 21, 2005
Posts: 34
How we are using OOP consept in Servlet code design. Where is inheritance and Polyformisam.

Be oN edGE SCJP 1.4, on edGE for SCWCD 1.4
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Servlet is the best example of OOP. Your web application is usually a number of screens, most of screens have something in common, some screens look similar to others. So you do the following:
1. define a base servlet taking care of common content and behavior of all screens
2. inherit servlets from the base to serve particular screens
3. build more complex hierarchy for servlets servicing similar screens, like reports, form processor and so on.
Using OOP reduces amount of developed code drastically. You need just override a few functions of base servlet and get completely different behavior. It's very powerful approach.

Maybe I should write a book about that, who knows?

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The actual servlet spec and classes are a small part of an application. You can apply OO Design Principles to your systems whether you start from a servlet framework or not. Read up on those principles and see the kinds of things we tend to care about.

If those make good sense, google for "MVC model 2" which is recipe for separating the responsibilities of servlets, JSP pages and a Plain Old Java Object business model.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
Wish to add this.

By overriding the service method itself you are doing dyanamic polymorphism.
Of course, this is achieved by none other than polymorphism.

Learning the OOPs concepts thoroughly and analysing how it is implemented in different Java technology will help you a lot

Sarath Mohan
It is sorta covered in the JavaRanch Style Guide.
subject: How use OOPs in Servlet
It's not a secret anymore!