Scriptlet Expression Declaration JSP Example

Scriptlet, Expression and Declaration are the three building blocks of JSP code. Let us see the appropriate places when to use them and finally we will do one example where all the three are used.

Note: It is advised to go through the separate examples on Scriptlets, expressions and declarations before going through this. Here, all the three are placed in one example.

1. Scriptlet

The general syntax of Scriptlet is <% some code %> and is the main coding area of JSP. One JSP file can have any number of Scriptlets, but each open <% should be closed with %>. The Scriptlet can be compared with the service() method of a Servlet, in the sense, large amount of code goes into the Scriptlet. Database access code can be written here (but the best place is JavaBeans for better reusability).

2. Expression

The general syntax of expression is <%= some code %> . It is used to place a small piece of dynamic code in static HTML. This can be done by Scriptlet also but for this purpose (of placing a variable value in running HTML) using Expression will be easier. One JSP file can have any number of Expressions, but each <%= should be ended with %>. Expression is evaluated and converted to a string and placed directly in the output going to client(in the response).

3. Declaration

The general syntax of Declaration is <%! some code %>. This is used by Programmer to declare variables and methods. These variables and methods are treated global that can be used by all parts of JSP coding like user-defined methods, Scriptlets and Expressions. That is, the code of declaration scope is all through the JSP file.

One JSP file can have any number of Declaration tags, but each open <%! should be closed with %>.

Important Note: All the 9 implicit objects are created in the service() method of the generated Servlet. That is, the implicit objects like out, request and response are local to Scriptlet and Expression. These three objects (for that matter, all 9 implicit objects) can be used by the Scriptlet and Expression straight away. But Declaration code is placed outside the service() method of the generated Servlet. For this reason, Declaration code cannot use these three objects (or all 9 implicit objects).

The service() method parameters are request and response and we create PrintWriter object in service() method itself. As request, response and out are local to service() and as Scriptlet and Expression code is placed in service() method, Scriptlet and Expression can use them without any problem. But as declaration code is placed outside the service(), the scope of request, response and out is not available to declaration.

This is very important to note in coding, else, compiler raises error.

Example on Scriptlet Expression Declaration JSP

Client-side HTML Program that takes user name, two numbers and send them to ThreeInOne.jsp on the server.

File Name: AllInOne.html

 

Using Scriptlet, Expression, Declaration in one Example

Enter User Name
Enter First Number
Enter Second Number

Server-side JSP file, File Name: ThreeInOne.jsp

<%!                        // 1st declaration
  String str;              // variables to retrieve client data of name and numbers
  int fn, sn, product;
%>
                           	
<%!                        // standard user name
  String stdUser = "Rao";  // infact, the two declarations can be made one. 
%>                         
				
<%                         // 1st scriptlet to extract client data
  str = request.getParameter("t1");          // str is global variable declared in first declaration
  String str1 = request.getParameter("t2");
  String str2 = request.getParameter("t3");  // str1 and str2 are local to scriptlet as they are declared here itself  and used
                           // convert (parse) str1 and str2 into integer values for arithmetic operations		
  fn = Integer.parseInt(str1);	
  sn = Integer.parseInt(str2);  	
  product = fn * sn;
 %>                        
                           
<%                         // infact, both scriptlets can be clubbed; it is just to show two scriptlets are 
                           //possible and access to global variables are possible from any number of scriptlets
  out.println("Accessing global variables from scriptlet:
"); if(str.endsWith(stdUser)) out.println("Product of " + fn + " and " + sn + " is " + product + ""); else out.println("You are not eligible to get product as your login failed"); %>
Accessing global variables from Expression
Your first number: <%= fn %>.
Your Second number: <%= sn %>.
Their product: <%= product %>.

HTML file screenshot when valid values are entered.

ima

Screenshot for valid values are entered.

ima1

Screenshot for invalid values are entered.

ima2

In Expression, user name is not validated and for this reason you get the product even when username failed. Sufficient comments are given then-and-there itself in the code for better understanding.

A similar example is available at Using Java Methods in JSP where all Scriptlet, Expression and Declaration are used along with Methods.

Leave a Comment

Your email address will not be published.