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

Using Declaration in JSP

Enter Student Name
Enter Student Marks

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

<%!				              // observe, declaration
   String str;
   String marks1;
   int marks;
%>

The following are student particulars using Scriptlet 
Student Status: <% // 1st scriptlet str = request.getParameter("t1"); // global str variable is assigned marks1 = request.getParameter("t2"); // global marks1 variable is assigned marks = Integer.parseInt(marks1); // parsing string to int marks if(marks < 35) // just some logic out.println("Student failed"); else out.println("Student Passed"); %> <% // 2nd scriptlet out.println("

Student name is " + str); out.println("
Student marks are " + marks); %>

Student name is <%= str %> and marks are <%= marks %>.

Screenshots of HTML file and JSP response

JSP Declaration

JSP Declaration

Let us see the code what it is doing.

<%!
    String str;
    String marks1;        
    int marks;
%>

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.

<%					     // 1st Scriptlet
    str = request.getParameter("t1");        // global str variable is assigned
    marks1 = request.getParameter("t2");     // global marks1 variable is assigned  

    marks = Integer.parseInt(marks1);	     // parsing string to int marks
%>

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.

<%				             // 2nd Scriptlet
     out.println("

Student name is " + str); out.println("
Student marks are " + marks); %>

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

   

Student name is <%= str %> and marks are <%= marks %>.

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.
    
         code fragment
    
    

    Example:

    
         

    Student name is <%= str %> and marks are <%= marks %>.
  6. Following code
    <%!				
        String str;
        String marks1;
        int marks;
    %>
    

    can be also written as

    <%!  String str;  %>
    <%!  String marks1;  %>
    <%!  int marks;  %>
    
  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 Comment

Your email address will not be published.