JSP Declaration with Example

JSP Declaration with Example


Note: It is advised to go through Scriptlet and Expression before proceeding this Declaration.

We know the three building blocks of JSP code are Scriptlet, Expression and Declaration.

Let us focus on JSP Declaration here. JSP Declaration represents the global area of a JSP page where you can write global (instance) variables and methods. These variables and methods can be accessed from any part of JSP page, being global.

Following is the syntax of JSP Declaration

<%! some code %>

The code of declaration goes outside the service() method of the generated Servlet, the global area. We know earlier, the code of Scriptlet and Expression goes into the service() method. For this reason only, Scriptlet variables are accessed from Expression and is shown in Expression example. The declaration code, being global, can be accessed by Scriptlets, Expressions and any user-defined methods.

Example on JSP Declaration

HTML Program, File Name: Student.html

Observe, client HTML file is invoking Dec.jsp.

The following JSP file reads the student name and marks and print them back using declaration, scriptlet and expression.

JSP File Name: Dec.jsp

Screenshots of HTML file and JSP response

JSP Declaration

JSP Declaration

Let us see the code what it is doing.

The code within the tags <%! and %> constitutes Declaration. In this declaration some global variables are just declared. They are given values in Scriptlet; reason being these variables scope is all through the JSP code.

The variables declared in declaration are given values reading from client HTML. request is one of the 9 implicit objects of JSP. getParameter("t1") method of request object returns always a string. That is, t2 field value is retrieved as a string into marks1. It is parsed to int marks. Here the question is not retrieving and parsing, but it is the visibility of code of declaration.

The above code is second Scriptlet. The Declaration variables are visible here also and for that matter visible in any Scriptlet.

The declaration variables are visible in Expression also.

Some salient points about JSP Declaration
  1. Declaration code comprises of variables (also known as fields) and methods. They can be used in the remaining part of JSP page or all through the JSP.
  2. Declaration syntax:

    <%! some declarations %>.
    Remember, Scriptlet is <% code %> and Expression is <%= code %>.

  3. JSP Declaration code can be used by Scriptlets, Expressions and user-defined methods.
  4. Variables and methods should be declared before they are used in the JSP file. Better place just below <body> tag.
  5. Following is XML version of declaration syntax.

    Example:

  6. Following code

    can be also written as
  7. The code of Declaration is placed outside any method (including service() method) in the auto generated servlet.
  8. JSP Declaration variables are instantiated only once when the first request comes, then for all the subsequent requests, the same variables are used.

Leave a Reply

Your email address will not be published. Required fields are marked *