This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sharing method between servlets" Watch "Sharing method between servlets" New topic
Author

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
Bartender

Joined: Mar 24, 2008
Posts: 3826

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.


SCJP 5 | SCWCD 5
[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]
 
Don't get me started about those stupid light bulbs.
 
subject: Sharing method between servlets