Difference in JSP <%! %> and <% %> tags

Dear Experts
  I would like to know the difference between the tags <%!   %> and <%  %> tags
with an example

(  Say )

    in my jsp , i  can declare a variable  like ,

    <%!

               int x=10;
    %>

  Also i can declare

  <%
           int x=10;
  %>


  What is the difference in the two different ways of declaring variables

Thanks and Regards
Karthick krishnan

   





karthickkrishnanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

w0lverCommented:
<% ... %>       Scriptlet code,  for embeding scripting code
<%= ... %>     Expression code, used to embed expressions when the result is added to the response
                       Also used to get request-time action attribute value
<%! ... %>       Declaration code, used to only declare instance variables and methods in the JSP implementation class

You use the declaration tag when you want to declare an instance variable.  A value that can be accessed from any method in the class and one that keeps its value when the method that sets it returns.  So a variable set by a declaration is shared by all requests for that page.  This practice is not thread safe.  If two requests come in at the same time they might have access the same exact value...

Yo can also use it to declare a method that can be used in scriptlets in the same page but I would recommend using a bean or a custom cation instead...  easier to maintain.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
Imagine your JSP page is compiled into a single function (by tomcat)

ie:

theJsp.jsp:
==========================
<h1>hello!</h1>
<%
    int x = 10 ;
%>

gets converted to:

public class theJsp extends JSPClass
{
    public void service( JSPWriter out )
    {
        out.println( "<h1>hello</h1>" ) ;
        int x = 10 ;
    }
}

if you use the <%! notation, it puts things outside the function (in the same class), ie:

theJsp.jsp:
==========================
<h1>hello!</h1>
<%!
    int x = 10 ;
%>

gets converted to:

public class theJsp extends JSPClass
{
    int x = 10 ;

    public void service( JSPWriter out )
    {
        out.println( "<h1>hello</h1>" ) ;
    }
}
0
jackjokerCommented:
with <%! int i = 15 %>, if you modify the value of "i", say you minus 5 and it becomes 10. The next time you refresh the page, it will still remain at 10, instead of being a new initialization of 15.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.