This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes static utility class from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "static utility class from servlet" Watch "static utility class from servlet" New topic
Author

static utility class from servlet

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
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

Joined: Jan 10, 2002
Posts: 60810
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
[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

Joined: Jan 10, 2002
Posts: 60810
    
  65

Not any more so than any other simultaneous access to the session or application context. Just something to keep in mind.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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

Joined: Sep 10, 2008
Posts: 188
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Yes. That's what I was thinking. Although I still wouldn't pass both the request and session parameters. One is redundant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static utility class from servlet
 
Similar Threads
problem updating quantity field
Connection Pooling - null pointer exception
thread safety check
passing hidden forms problem
Iterator in Struts 1