aspose file tools*
The moose likes Servlets and the fly likes Use of <%!  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Use of &lt;%!  " Watch "Use of &lt;%!  " New topic
Author

Use of &lt;%!

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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


Regards,

Pho
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Apr 20, 2000
Posts: 3252
<%! %> 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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Apr 18, 2001
Posts: 46
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


Vikas Aggarwal<BR>Technology Associate<BR>http://www.vikinsa.com
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
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.


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of &lt;%!