• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static utility class from servlet

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic