File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Methods in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Methods in Servlet" Watch "Methods in Servlet" New topic

Methods in Servlet

Ripan Karmakar

Joined: Jul 28, 2004
Posts: 16
Can we use any user defined methods in the Servlet like as we define/use in Java.........if so any one reply with an example......



"Strenght is Life and Weakness is Death"<br />Java/J2EE Developer<br />UshaComm Pvt. Ltd, Kolkata, INDIA
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Sure you can. But for the most part, you will want to put those methods in a Java Object that is agnostic to your Servlet. That's better design anyway.

GenRocket - Experts at Building Test Data
Navin Ramineni

Joined: Jul 28, 2004
Posts: 3
Becareful with the method names. For example, if you override the service() methods in your servlet class, you will lose the functionality provided by the HttpServlet Class and the doXXX methods will not be called automatically
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Preferably make those methods private.

Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
You can use methods in a servlet just the way you would use it a normal Java class. Quite a few times (make that most of the times if you wish), you will need to a few operations within your request handler methods. There is nothing to stop you from writing a method, public or private according to your app design, and calling it from your service methods.

However, as Gregg has rightly pointed out, having Java classes with specific responsibilities is way much better OOP. Service methods can use these objects to process user requests.

The bottom line is, yes you can definitely use methods inside your servlets just the way you do in normal Java classes

Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Manish Vyas

Joined: Jul 21, 2004
Posts: 13
Mr. Rao, you mean we can also use java methods in Servlet without doXXXX() method... Please clarify.
I agree. Here's the link:
subject: Methods in Servlet
It's not a secret anymore!