JSP - SPLessons
boy2 Chapter 8

JSP Declaration

JSP - SPLessons

JSP Declaration

shape Description

JSP Declaration tags are used to declare variables, methods, fields. The tags are written inside the JSP page and placed outside the _jspService () and are auto generated by the JSP container. In JSP static member, instance variables can be declared in the declaration tag. The declaration of declaration tag can be represented as follows.

<%! declaration %>

JSP Declaration – The fundamental contrast between the declaration and script is that exclusive variables will be composed inside the JSP Scriplet and set within _jspService (), whereas in JSP Declaration both variables and methods will be composed and put outside the _jspService ().

shape Conceptual
figure

The advantage of declaration tag is that it can have variables and methods.

shape Syntax

<% ! instance variable, user defined methods, init(), destroy()  %>

1.Declaring int datatype<%!  int a = 10; int b = 20; %>
2.Declaring String datatype<%!String name = “Mark”; String date = “28th April, 2004”;%>

shape Example

declarationTag.jsp

<html>
 <body>
     <%! String name(String n){ return (n); } %>
     <%= "your name is :"+name("SPLESSON") %>
  </body>
</html>

Inside the body tag one can write the HTML code or JSP code, both will get execute on the server. Where in the JSP declaration tag mentioned String to display the message that written in the code.

<%! String name(String n){ return (n); } %>

Where in the scriplet tag mentioned the output line.

<%= "your name is :"+name("SPLESSON") %>

Output

After executing the above code successfully, output will be as follows.

Difference between Jsp Scriptlet Tag and Jsp Declaration Tag

Jsp Declaration Tag Jsp Scriptlet Tag
The jsp declaration tag can declare variables as well as methods. The jsp scriptlet tag can only declare variables not methods.
The declaration of jsp declaration tag is placed outside the _jspService() method. The declaration of scriptlet tag is placed inside the _jspService() method.

Summary

shape Key Points

  • JSP declaration tags will be placed out side the _jspService() method.
  • Inside the declaration tag variables and methods will be written.
  • While requesting  declaration tag won’t get memory.