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 Web Component Certification (SCWCD/OCPJWCD) and the fly likes SingleThreadModel Servlet with class variable 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "SingleThreadModel Servlet with class variable" Watch "SingleThreadModel Servlet with class variable" New topic
Author

SingleThreadModel Servlet with class variable

Jo Liang
Greenhorn

Joined: Dec 10, 2001
Posts: 11
When a Servlet implments SingleThreadModel then the local, instance and request variables are thread safe.
Therefore, I think I could said the belowing serlvet, which implements SingleThreadModel and has a instance variable, is thread safe, right?

----------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet implements SingleThreadModel {
public String var; //Instance variable
public doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServeltException{
.....
}
}
----------------------------------------------

But can we say the Servlet is ThreadSafe if it implements SingleThreadModel but has a "class variable"??
Like this:
----------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet implements SingleThreadModel {
static public String var; //class variable
public doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServeltException{
.....
}
}
----------------------------------------------
Is this Servlet is ThreadSafe? Cause I think class variable is never thread-safe.
Thank you.
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
You right, class variable is not thread safe although we implement SingleThreadModel.
Web container may created some instances of that servlet but class variable is created only one fot those instances.
Correct me if I am wrong
daniel
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
You are right, Daniel.
I do not think, that the term "Threadsafe servlet" is a good choice here.
We do not not know, what the example servlets do in their service-methods. Maybe, they access the servlet context without synchronization, ...
Greetings from Hamburg,
Stefan


Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SingleThreadModel Servlet with class variable