• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Declarations

 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi our J2EE instructor said that variables declared using JSP Declarations, is not thread safe
so <%! private int hitCount = 0;

goes into the Servlet class during translation but not into the _jspService(). SO it's not threadsafe.
So if that is true, is there a reason why one would want to declare variables that way? and is there a way around it?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if that is true, is there a reason why one would want to declare variables that way?


It is true.
You would declare variables that way if you want all the threads to have access to the same object.

If you are developing your apps using an MVC architecture, there is no need
to declare variables in a JSP anyway.
In a servlet, it's not unusual to see init params declared as instance variables and then populated in the init method. Such variables only get read and never changed once the app is running anyway.

and is there a way around it?

Way around what?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and is there a way around it?


I think you are curious to know that how to make them thread safe. Right?

If so, then the answer would be, you have to takecare of it. You need to make it thread safe yourself. May be by making it final, by using synchronized block.

[edited]
But MVC Model 2 is your best bet. You should not go for declaring variables in your JSP.
[ June 21, 2005: Message edited by: Adeel Ansari ]
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that was my gut feeling, that one should not be declaring variables in JSP. But since the mechanism was there, I wanted to be sure.
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!