File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes making a JSP page Thread safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "making a JSP page Thread safe" Watch "making a JSP page Thread safe" New topic

making a JSP page Thread safe

Yogender Butola

Joined: Jul 19, 2004
Posts: 12
How will I make session variables thread safe in a JSP page?
and How can I create a method in a JSP page. Is it possible? If yes then pls let me know?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One trick is to move all the code out of your servlet into a threadsafe class:

WorkerClass is not shared by multiple threads the way Servlet is, so it can use member variables. The vendor framework I'm using now does this. There is essentially one servlet that gets the right worker class for the request from a factory.

What's the downside? They must have designed servlets the way they are for some reason. The servlet design - one instance shared by many threads - reduces object creation and destruction. This trick guarantees one object created per request. How much does that matter? Anybody know? Any other pros & cons?

Editing - I just noticed you asked JSP and I answered Servlet. I haven't written enough JSP to know just how much the difference matters ... waddya think?
[ July 19, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
How will I make session variables thread safe in a JSP page?
and How can I create a method in a JSP page. Is it possible?

Variables held in a session are isolated from all other user sessions - the only time that they are not completely thread safe is if you are processing multiple requests from the same user at the "same time" -as might occur with a frameset.
For creating a method in a JSP, I recommend you read a good tutorial on JSP. Here is a good place to start. I recommend the JSP "Reference Card" - very handy.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Don't use JSPs for anything except displaying information, do all the rest in servlets.

How often does that mantra need to be repeated?
Why did Sun ever include the capability for scriptlets in the JSP spec?
Why do people keep posting JSP questions in the servlet forum?
[ July 20, 2004: Message edited by: Jeroen Wenting ]

I agree. Here's the link:
subject: making a JSP page Thread safe
It's not a secret anymore!