File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Traps?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Traps??" Watch "Servlet Traps??" New topic
Author

Servlet Traps??

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hello all,
Where can i have the info regarding Servlet Traps??
One trap that i have encountered is, servlet instance variables are not Thread Safe. Is this statement true for all Servlet engines??
Please provide me links to info on Servlet Traps
Thanks,
Sajee Joseph.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Yes, all servlet engines will have instance variables that are not threatsafe.
This is inherent in the way a servlet engine works.
When a servlet is first called (or when the servlet engine starts, depending on the settings in web.xml) a single (or multiple, depending on server options and configuration in some servlet engines) instance of a servlet class is created.
From this instance multiple threats are launched when simultaneous requests need to be handled, one threat for each request (think of the service() method as a run() method in a multithreaded application).
Therefore each of those threads (and thus all requests) will have access to the same instance variables!


42
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It is also possible, although generally not considered a good idea, to mark a servlet class as implements SingleThreadModel. This will have the effector forcing the servlet container to only pass a single request through it at any one time.
See this thread from a few weeks ago for more details.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Kevin Jones
Author
Ranch Hand

Joined: Oct 29, 2003
Posts: 39
But remember that SingleThreadModel is deprecated in the 2.4 specification


Kevin Jones<br />Author: <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0321136497/jranch-20" target="_blank" rel="nofollow">Servlets and JSP: The J2EE Web Tier</a>
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Sajee,
Google: servlet "best practices"
You should get a lot of useful information.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Kevin Jones:
But remember that SingleThreadModel is deprecated in the 2.4 specification

Hi Mr.Kevin,
I just would like to ask a question... The SingleThreadModel is deprecated in Servlet 2.4 Spec, but the JSP page directive still has the attribute "isThreadSafe"... As far as I know, if we set that attribute's value to be false, then the generated servlet class will implement the SingleThreadModel, won't it? So does it mean that the generated servlet will still contain the deprecated interface implemented?
So I just would like to know if there is any other way to avoid the deprecated interface in the generated servlet....
Thank you, Mr.Kevin...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jayson Falkner
Author
Ranch Hand

Joined: May 07, 2001
Posts: 57
Straight from the JSP 2.0 spec:
isThreadSafe,
Indicates the level of thread safety implemented in the page. If false then the JSP container shall dispatch multiple outstanding client requests, one at a time, in the order they were received, to the page implementation for processing. If true then the JSP container may choose to dispatch multiple outstanding client requests to the page simultaneously. Page authors using true must ensure that they properly synchronize access to the shared state of the page. Default is true. Note that even if the isThreadSafe attribute is false the JSP page author must ensure that accesses to any shared objects are properly synchronized., The objects may be shared in either the ServletContext or the HttpSession.
The JSP 2.0 spec doesn't require that the SingleThreadModel interface is used (although it certainly is implied). The isThreadSafe attribute is meant to inform a container that a JSP needs to handle requests one at a time -- which should never be something you have to do. If a generated servlet implements the SingleThreadModel interface is up to the specific container that generates your servlet.
A pretty good way to ensure you never end up with a generated servlet that implements SingleThreadModel is to never set the isThreadSafe attribute to false.
If you are interested in why SingleThreadModel is bad (and why it was originally made) and other gotchas similar to it, check out Kevin's and my book. We devote a whole chapter to maintaining state in a web application, which deals with the SingleThreadModel issue and similar issues, and all of the other chapters are packed with helpful tips, things to avoid, and good practices.


Jayson Falkner<br />jayson@jspinsider.com<br />Author of <a href="http://www.jspbook.com" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Jayson Falkner:
We devote a whole chapter to maintaining state in a web application, which deals with the SingleThreadModel issue and similar issues, and all of the other chapters are packed with helpful tips, things to avoid, and good practices.

Thank you for your great explanation on SingleThreadModel and by the way, does the book discuss about the best practices for building web application, optimization and performance issues? If so, is it somehow categorized into advance level books???
Thank you again for your explanation...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Traps??