Scriptlet Expression Declaration JSP Example

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

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

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 Reply

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