This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Sharing method between servlets 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 "Sharing method between servlets" Watch "Sharing method between servlets" New topic

Sharing method between servlets

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I have the following method in my servlet:

I am now working on another servlet which will also be using this method. I want to know the best way I can share it between both servlets. I was thinking of creating a final class and make the method a static method. Is this a good way of dealing with this issue ?
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

You should move that part (only, code which can be reused and not the things usually go in servlets like setting the content type etc.. ) in to a separate class and let the servlets (or other components) call it, in that way any changes for that can be made easily later.

[How to ask questions] [Twitter]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

You can have a BaseServlet and move the method to it, than let your other servlets extend it.

[Servlet tutorial] [Servlet 3.0 Cook Book]
I agree. Here's the link:
subject: Sharing method between servlets
It's not a secret anymore!