Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing method between servlets

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have a BaseServlet and move the method to it, than let your other servlets extend it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic