Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 41086
848
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: 67637
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: 67637
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: 41086
848
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: 41086
848
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.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic