• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Declaration and Scriptlets!  RSS feed

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
JSP declaration is like <%! ... %> and scriplets would be like <% .. %>
whatever is there in declaration gets inserted into our servlet and whatever is there in the scriptlet is inserted into the service method of the servlet.
i know that if this the case then we should not put variables that might be shared across multiple servlet threads into the declaration. but still if someone can give me an example of usability of both of these then it would be great help to me.
if i'm to give an example of this then i would say that "if we want to determine the how many requests came for the same JSP page then we can put a counter variable into the declaration which becomes a part of the sevlet instance and so we can increment it in the scriptlet code to keep counter updated". like,
<%! int counter = 0; %>
<% counter++; %>
am i right?
regards
maulin.
 
Srini Admala
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example is not thread safe for there could be multiple threads trying to update the counter.
You need to have "synchronized" access code for such variables.
-Srini
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!