Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

JSP Declaration and Scriptlets!

 
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.
 
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
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic