JSP Declaration

In JSP declaration tag two or more methods or variables are declared. These methods or variables can be used in the Java code and in the JSP file. Variables must be declared first so that they can be used in the JSP file. When the JSP file is translated into servlet class, the variables or methods that are declared inside the declaration tag will be declared inside the servlet class but not inside any methods of servlet. We can declare an instance variable or method or a static variable or method inside the declaration tag of JSP.

SYNTAX:

The following is the syntax of JSP declaration:

<%! declaration %>

EXAMPLE:

Consider the following example to declare a variable in a JSP page using JSP declaration:

<%! int x = 0 %>

In this statement x is declared as an integer type variable and is initialized at 0.

Consider the following example in which value of n is printed and n is declared and initialized inside the declaration tag:

CODE:

< html >

< head >

< title > JSP Page < /title >

< /head >

< %!

       int n = 1;

   % >

< body >

        Value of n:

< % out. println (++n); % >

< /body >

< /html >

When the above JSP code is translated into servlet the declaration of n will be outside from the service method of the servlet class, the following will be the servlet class:

SERVLET:

public class abc_jsp extends HttpServlet

{

int n =0;

public void _jspService (HttpServletRequest request, HttpServletResponse response)

                               throws IOException, ServletException

   {

      PrintWriter out = response.getWriter ();

      response. setContenType (“text/ html”);

      out.write (“< html>< body >”);

      out.write (“Value of n”);

      out.print (++ n);

      out.write (“< /body >< /html >”);

   }

}

It can be seen that in the servlet variable n is outside the service method, this is because we used the declaration tag to declare n. If we have declared the variable n using scriptlet tag then in the servlet the variable n will become inside the service method. This is done before in the previous sections.

 

When to use declaration tag and not scriptlet tag:

The declaration tag is used when the user wants to include the methods in his JSP file. When the JSP file is translated, the variables and methods that were declared inside declaration tag become the instance variables and instance methods and these are also assigned default values. Therefore, if you want to include the variables and methods in JSP file you must declare them by using the declaration tag.

Consider the following example in which we have declared a variable and a method inside the declaration tag:

CODE:

< html >

< head >

< title > JSP Page < /title >

< /head >

< %!

int n = 1;

int getValue() {

system. out. println (“The getValue method”);

return n;

}

% >

< body >

Value of n:

< % out. println (getValue ()); % >

< /body >

< /html >

In the above code we have variable n and method getValue (). Both are declared inside declaration tag. In the print statement we were printing the value of n before but now we are calling the getValue () method.

When this code is translated into servlet, the variable n and method getValue () will become instance variable and instance method. The following is the servlet of the above code.

SERVLET:

public class abc_jsp extends HttpServlet

{

int n = 1;

int getValue()

{

system. out. println (“The getValue method”);

return n;

}

public void _jspService (HttpServletRequest request, HttpServletResponse response)

                               throws IOException, ServletException

   {

      PrintWriter out = response.getWriter ();

      response. setContenType (“text/ html”);

      out.write (“< html>< body >”);

      out.write (“Value of n”);

  out.print (getValue ());

      out.write (“< /body >< /html >”);

   }

}

It should be noted that we cannot add any function inside the scriptlet tag this is because when the JSP page will be translated the function will go inside the _jspService () method and if we added the getValue () method inside the scriptlet tag it will go to _jspService method on compilation but in java programming we cannot add any function inside another function.