JSP - SPLessons
boy2 Chapter 10

JSP Scriptlet

JSP - SPLessons

JSP Scriptlet

shape Introduction

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.

shape Conceptual
figure

JSP Scripting Tag

shape 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.

shape Syntax

<% java code %>

JSP Expression Tag

shape 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.

shape Syntax

<%= java expression %>

JSP Declaration Tag

shape Description

In declaration tag methods, varaiables can be declared. Static functions and instance variables can be declared.

shape Syntax

<%! declaration %>

shape Example

Following is an example by using syntax.

index.jsp

<html>
	<body>
			<h2>
			<% int a=20; int b=10; int c = a+b; out.println("result is :" +c); %>
			</h2>
	</body>
</html>

Output:
Here the developer used three variables a, b, c where c is used to add two values.

 compare.jsp

<% int a = 20; int b = 10; if(a>b)
     out.println("a is bigger");
   else
   out.println("b is bigger");
%>

Output:
It displays the bigger element.

Summary

shape 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.