• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static utility class from servlet

 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im pretty sure its ok but is it thread safe to use a static utility class from a servlet as long as you pass in the session or request object?
I assume that if there is all member variables and no global ones everything is thread safe to process some code in a utility from a servlet like so?

thanks



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I would generally not make the methods static, but that is a design thing not a rule.

Also, note you don't need to pass both request and session into the same method as you can call a method on each to get the other.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your static methods do not operate on any instance data (other than what you are passing in) there should be no threading issues in using your static methods.

I use a couple of "utility" classes full of static methods in my servlets. Since there is no instance data or initialization to worry about, I find them incredibly easy to reuse. And also since the utility class has no "state" of it's own, I am all but guaranteed that passing in the same parameters will always yield the same result.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Yes. I would generally not make the methods static, but that is a design thing not a rule.

Also, note you don't need to pass both request and session into the same method as you can call a method on each to get the other.


i see. Would you make it a singleton instead and call getInstance() ow would you attack it a different way?

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Nelson wrote:Since your static methods do not operate on any instance data (other than what you are passing in) there should be no threading issues in using your static methods.


Not quite true. Simultaneous access to the session and application context can still cause contention.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Bear Bibeault][quote=Pete Nelson]Since your static methods do not operate on any instance data (other than what you are passing in) there should be no threading issues in using your static methods.[/quote]
Not quite true. Simultaneous access to the session and application context can still cause contention.[/quote]

so then would some of those methods i created be not thread safe?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not any more so than any other simultaneous access to the session or application context. Just something to keep in mind.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Schretz wrote:i see. Would you make it a singleton instead and call getInstance() ow would you attack it a different way?

A different way. I'd create a class called something like ShipperHelper and have it take an HttpRequest into the constructor. I'd instantiate it each time a request was made. I'd also refactor some methods like setShipSelector() that have a lot of logic into smaller methods and being able to have state might help on that front.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
John Schretz wrote:i see. Would you make it a singleton instead and call getInstance() ow would you attack it a different way?

A different way. I'd create a class called something like ShipperHelper and have it take an HttpRequest into the constructor. I'd instantiate it each time a request was made. I'd also refactor some methods like setShipSelector() that have a lot of logic into smaller methods and being able to have state might help on that front.


Ok, How does this look? Was this along the lines of what you were saying?

thanks
john


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That's what I was thinking. Although I still wouldn't pass both the request and session parameters. One is redundant.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic