I always get confused about thread safety and servlets. I know multiple threads call doPost or doGet. here is an example: public class MyServlet extends HttpServlet { privateHelper help;
public void init() throws ServletException { help = new Helper(); }
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { help.helperMethod(req); } } Helper is an instance variable of the servlet (which usually means not thread safe) but here the Helper class does not store state, the methods get the state needed passed in. Is this thread safe? would it be better if i removed the class variable help and inside doPost i did new Helper.helperMethod(req); how about a static method instead: Helper.helperMethod(req);
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12266
1
posted
0
Does Helper have any instance variables that change between users? If Helper only uses local variables then you are safe. No reason to make a new one unless there is a problem with instance variables. Static would have the same effect as your single member - seems to me. Bill
While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway. For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.
Customer surveys are for companies who didn't pay proper attention to begin with.
Originally posted by Tim Holloway: While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway. For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.
Actually there are a lot of reasons to use instance methods in a stateless helper object. Foremost among these is that you can subclass your helper class and override the methods -- something you can't do with class methods. Kyle
Hello, Why does the servlet need to keep a reference to the helper class as an instance variable? Why not just create a new local helper class in the method that it is needed? If you are concerned that creating new helper objects when needed will have a performance hit then you could always have a pool of helper objects that would then be available to all servlets rather than just tying the object to a single servlet. Thanks, Steve
Gerry Giese
Ranch Hand
Joined: Aug 02, 2001
Posts: 247
posted
0
I think everyone here caught a part of it. I believe we have three different situations where a helper class may be used, and therefore several ways (patterns?) of implementing a solution. Anyone/everyone please correct me if I'm slightly misguided (or just flippin' wrong!). Case 1 --------- Helper is doing processing related to all users of the system. Example: Hit counter. Solution: Use a Helper with a static member variable, and make the method used to change the variable synchronized in order to make it safe and produce correct results. An optional implementation is rather than making the Helper a member variable of the servlet, create it in the init() but place it in the ServletContext, and retrieve it in the doXXX processing. This would allow the Helper to be accessed by other servlets/JSPs as well. Case 2 --------- Helper performs simple logic or utility processing on request input. Example: Form Validation. Solution: Instantiate a new Helper object in the doXXX method and use it as needed. Do not use static member variables in it. Case 3 --------- Helper performs complex logic or utility processing on request input that involves creation of other objects to aid processing, or Helper needs to be part of a framework or extensible class hierarchy that supports/enforces certain functionality or the particular Helper do be used is picked dynamically based on request input or session/application state. Example: Form validation with database update using a logging class and a database connection factory. Solution: Write a Helper factory that creates and/or pools Helper objects. Additional objects that support the Helper (HelperHelpers?) may also need to be retrieved from a factory depending on requirements.
CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)<br />Author of <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi" target="_blank" rel="nofollow">Posts in the Saloon</a>
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.