Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making a JSP page Thread safe

 
Yogender Butola
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic