This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will be the output of this servlet?
 
Similar Threads
Increasing Array Size
where to place Bean file(*.class)in Tomcat server
Uploading multiple image files
opening a pdf file created by jasper in client side
some basic doubts in web services