• Post Reply Bookmark Topic Watch Topic
  • New Topic

incorporating a method into all servlets  RSS feed

 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have a bunch of servlets on a project.

I want to add the following servlet to all of them. --My question is how would I do that? -- Thanks,

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:I want to add the following servlet to all of them. --My question is how would I do that? -- Thanks,

That's not a servlet, it's a method. Are you asking how to add the method to all servlets? If so, have all the servlets extend a class that contains the method. This is basic Java.

P.S. WHen posting code, please do not provide your own line numbers. Let the code tags do that.
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok got it thanks,
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:Hello I have a bunch of servlets on a project.

I want to add the following servlet to all of them. --My question is how would I do that? -- Thanks,

Ding! Ding! Ding! Ding! Ding! ... -- That's the sound of my copy/paste danger warning going off. Why would you add the same method in multiple places? That's creating duplication. Good programmers spend a lot of their time eliminating duplication. You need to encapsulate the logic in one class, then have your servlets collaborate with that class to get something done.

The reason I recommend having a separate class to take care of that logic is because the code doesn't look like anything that a servlet should actually be concerned about. Why make a servlet do something it wasn't really intended to do? Servlets sit between your business logic and the user so that they can act as a go-between and funnel requests from the user to the appropriate objects in your application that are actually responsible for doing the business work. Servlets are for infrastructure-related work. You don't ask the receptionist in your office lobby to calculate the salaries for your employees, do you? Same deal with servlets -- they are the receptionists of your web app.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The question that should have been asked first is: should this method be added to all servlets?

To that, I would say no. This is the sort of thing that would be better encapsulated by a helper class rather than being "added" to the servlets themselves.
 
Michele Smith
Ranch Hand
Posts: 421
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay but my application has hundreds of servlets.

Without using the dispatcher method, how can I effectively link this method encapsulated in its own class, to those hundreds of servlets?

Is making it a helper class sufficient to do this? Should my efforts be geared to making those hundreds of servlets use a helper class?

Thank you, and I am sorry if I am really spacy sometimes Bear, you are very helpful and I am grateful for your advice, thanks again,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does every single servlet need to call this? In any case, what's the difference between modifying the servlets to use the method within its own hierarchy versus to use the method in a helper class? You'd still need to edit any servlet that needs to call the method.
 
Nauman Hasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the question is what are you trying to accomplish by adding this method? If you need to know the clients's aspect ratio then an alternative could be to put the logic in a filter and then cache the value in the session to retrieve when you need it. This way you will only need to calculate it once on the client's first request.

~Nauman
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So just out of curiosity, why are hundreds of servlets concerned with an aspectRatio? Does this have anything to do with the presentation of the web pages? If it applies to all requests coming in, you might want to look at creating a Filter class. Otherwise, if it's part of some calculation related to the business logic that your application handles, then you have quite a problem. I still wouldn't try to put that code in the servlets, regardless of whether you can make it work using a superclass and making all your servlets extend it (not to give you any ideas because that's a BAD idea)
 
Daniel Val
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michele Smith wrote:Okay but my application has hundreds of servlets.

Without using the dispatcher method, how can I effectively link this method encapsulated in its own class, to those hundreds of servlets?

Is making it a helper class sufficient to do this? Should my efforts be geared to making those hundreds of servlets use a helper class?

Thank you, and I am sorry if I am really spacy sometimes Bear, you are very helpful and I am grateful for your advice, thanks again,


Create a base class for your servlets and add this method. If you don't want to do that because the method is in some helper, you can either call it from your servlets directly referring the helper, or if you don't like this, still create a servlet base class, a method in that class that will delegate the call to your helper.

D

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!