Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How to make jsp with instance variable thread safe?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
why instance variables in jsp isn't good practice?

My book says value of instance variables in jsp may be
read and updated at the same time by multiple threads
so jsp won't be thread safe anymore if we use instance
variables.........
can anyone give one example???

Is there no other way to make jsp with instance variables Thread safe???
If yes how???

thanks in advance
 
Marshal
Posts: 67344
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is 2014, not 2001. There should be no Java code in a JSP at all. None. No instance variables, no Java code.

You should be using the JSTL and EL and not writing JSP like it was written over 12 years ago.

If this is a legacy application from that time, then do not introduce instance variables. They will cause problems.
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also we shouldn't forget that JSPs are translated into servlets and the container creates only one instance of the servlets. Every time we send a new request, a new thread is created.
 
Bear Bibeault
Marshal
Posts: 67344
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is exactly what causes the thread-safety issue. Any values stored in instance variables will be overwritten by other requests.

There are many good reasons that putting Java scriptlets in a JSP is obsolete. This is just one of them.

Bottom line: write JSPs correctly and there are few opportunities for threading issues.
 
RabiDas Sharma
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Suppose i want to count the no. Of requests made to a jsp page then how can i do that if i'm not supposed to use instance variables in jsp page
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put that code into the controller that handles access to the JSP (assuming there is one, which, of course, there should be - MVC has been standard practice for a very, very long time by now).

If there isn't, filters would be an option.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!