[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-22
5
Medium Priority
?
262 Views
Last Modified: 2010-05-18
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

   





0
Comment
Question by:karthickkrishnan
3 Comments
 
LVL 3

Accepted Solution

by:
w0lver earned 100 total points
ID: 12645239
<% ... %>       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
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 100 total points
ID: 12645394
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
 

Expert Comment

by:jackjoker
ID: 12760410
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question