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?
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.
OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
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.
[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?
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 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?