wood burning stoves 2.0*
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SingleThreadModel Servlet with class variable
 
Similar Threads
Doubt in Thread Safe Servlet.
Mock Question
is this thread safe?
Is the following servlet thread safe?
Is it thread safe?