This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help in jsp declaration and thread safety?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am reading Head first servlet and jsp book. i have learned that in a servlet instance variables are not thread safe and a well written servlet wont have any instance variables. in the chapter of jsp the book says that a jsp becomes a servlet. also using jsp declarations <%! > we can declare instance varialbes, static variables etc. so how does this thing fits into the earlier part that a well written servlet does not or should not have instance variables ? or are instance variables declared using jsp declarations thread-safe ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java code in JSPs has been obsolete and discredited for over 10 years now -- why are you still using them?

But to answer the question, yes, creating declarations can make a JSP non-thread-safe. Don't do it.
 
Stefan Evans
Bartender
Posts: 1746
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It fits in exactly.

Using <%! scriptlet declaration tag %> you can declare an instance variable for your jsp/servlet.
It is exactly the same deal as declaring an instance variable in a servlet: they are not thread safe, and are thus their usage is discouraged.

In fact, scriptlets in general on a JSP page are discouraged. Instead you should be using EL and custom tag libraries (like JSTL)
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear and Stefan. and yes i know that scripting elements in jsp are not good and we should be using EL and JSTL. im preparing for ocewcd 6 certification and right now just at the start of jsp chapter whereby author is telling me various scripting elements and where they land in the container generated servltet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic