This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes What will be the output of this servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What will be the output of this servlet?" Watch "What will be the output of this servlet?" New topic
Author

What will be the output of this servlet?

Deep Krishna
Greenhorn

Joined: Jun 11, 2001
Posts: 19
Hi,
I have a servlet as follows. When i execute this servlet (each client request is in different browser), will the result (value of j for each client) predictable. Assuming non-distributable environment.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet implements SingleThreadModel
{
int i = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
int j = ++i;
out.println(j);
}
}

thanks
deep
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
The values would be different. To test it simply start your browser up multiple times
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
I believe the answer is no it's not predictable. Remember that each request is a separate thread. Since the getting and incrementing of i is not synchronized it is possible (not likely, but possible) for odd things to happen like one thread incrementing i a second time before j is printed.
The moral of the story is NEVER update instance variables in servlets! Servlet variables should only be read-only.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Deep,
Since you are using the SingleThreadModel for your servlet, you won't have any synchronisation problems. Anyway, the results predictability only depends on the container and whether you are using a pool of instances or only one intance. In case of one instance, it will be predictable.
I also agree with Kyle, that it is never a good idea to update instance variables inside a servlet, because of two problems:
- forces synchronisation
- prevents the use of a pool of instances for handling requests
Cheers,
Beno�t
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Good point Benoit. I completely missed that he was using SingleThreadModel (sound of hand slapping forehead). In that case it will be deterministic as you say.
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
 
subject: What will be the output of this servlet?