• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of <%!

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the difference between using
<! ... %>
and
<% %>
If I want to declare a variable and initialized it, which
one of the two should I be using ?
Pho
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Java source code generated to see the difference.
<%! %> declares an instance variable or member method
<% %> creates a code fragment inside the service method of the JSP - for example, a local variable.
Bill

------------------
author of:
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! %> declares an instance variable or member method
Think twice before you use this - it instantly makes a JSP thread-unsafe. You would be forced to either handle the threading issues yourself, or to decrease the scalability of your application by marking the page as thread-unsafe.
- Peter
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think twice before you use it to declare a member variable (rather than a method), I mean. (Sorry, can't edit the post for some reason).
- Peter
 
vikinsa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yopu can use <% %> to declare variables too. Though it is used to write in the code fragment. e.g.
<% String strText=null;
Vector vctVector=new Vector();
%>
Originally posted by Pho Tek:
Hi,
What is the difference between using
<! ... %>
and
<% %>
If I want to declare a variable and initialized it, which
one of the two should I be using ?
Pho


------------------
Vikas Aggarwal
Technology Associate
http://www.vikinsa.com
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything within <%! ... %> will end up in the generated Servlet's class body, not in the body of the request handler method. If you define variable within this block you must ensure access from the request handler is either limited to read-only, or synchronized in order to make then thread-safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic