JSP Scriptlet- Basically writing Java code in HTML tags is very difficult. In JSP, script tag is used to write JAVA related code within a JSP page. For use scriptlet tags, scripting elements must be known. Java code can be kept between. A scriptlet is a valid code in Java and is put into the jspService () method of the JSP engine at the time of running. The Scripting elements are written in Java code in the JSP page. Object creation and accessing will be done by elements of the script.
Following are the types of Scripting Elements
Expression Tag
Declaration Tag
Scriptlet Tag
Conceptual
figure
JSP Scripting Tag
Description
The Java code written in a scriptlet tag goes to _jspService () method of a JSP that is equivalent to servlet program.
Business logic or requesting processing logic code can be placed in the scriptlet tag of JSP program.
In a scriptlet tag, implicit objects are visible and used to perform JSP related programs.
Variables will be initialized in JSP as a local variables of _jspService() method in JSP program.
Syntax
<% java code %>
JSP Expression Tag
Description
To read the values of any functions like methods or variables Expression Tag will be used, out.print() used to read the information in expression tag.
Syntax
<%= java expression %>
JSP Declaration Tag
Description
In declaration tag methods, varaiables can be declared. Static functions and instance variables can be declared.
Syntax
<%! declaration %>
Example
Following is an example by using syntax.
index.jsp
[java]
<html>
<body>
<h2>
<% int a=20; int b=10; int c = a+b; out.println("result is :" +c); %>
</h2>
</body>
</html>
[/java]
Output:
Here the developer used three variables a, b, c where c is used to add two values.
compare.jsp
[java]
<% int a = 20; int b = 10; if(a>b)
out.println("a is bigger");
else
out.println("b is bigger");
%>
[/java]
Output:
It displays the bigger element.
Summary
Key Points
JSP Scriptlet - Scriplet tag is used for executing java code in JSP.
JSP Scriptlet - Scriptlet tags are easy to write.
JSP Scriptlet - out.println() is used for scripting languages.