JSP Scripting Elements


This article is a series of the JSP Tutorial. In this article, we will learn important JSP scripting elements with examples.

The scripting elements provide the ability to insert Java code inside the JSP.
In JSP, there are actually different types of scripting elements.
  1. JSP expressions
  2. JSP scriptlets
  3. JSP declarations
The below diagram shows the summary of using these scripting elements:

Let's discuss each scripting element with its syntax and examples.

1. JSP expressions

We use JSP expressions to compute some type of expression, and the result is included in the HTML page that's returned to the browser.
The expression tag evaluates the expression placed in it, converts the result into a String, and sends the result back to the client through the implicit response object.

The Syntax of JSP Expression Tag

<%= expression %>

JSP Expression Tag Examples

Here is a sample snippet that basically converts a string to all upper case.
<html>
   <body>
 Converting a string to uppercase:
 <%=new String("Hello World").toUpperCase()%>
   </body>
</html>

2. JSP scriptlets

A scriptlet is a set of Java programming statements embedded in an HTML page. The statements are distinguished from their surrounding HTML by being placed between <% and %> markers, as the following shows:
<% statement; [statement; …] %>

Example of JSP scriptlet tag

A scriptlet element allows you to write Java code that is executed when the JSP page is requested.
In this example, we are displaying a HelloWorld message.
```jsp
<html>  
   <body>  
       <% out.print("HelloWorld"); %>  
   </body>  
</html>  

3. JSP declarations

A declaration element allows you to define variables and methods that are accessible throughout the JSP page.

The Syntax of the JSP Declaration Tag

<%!  Declaration %>

JSP Declaration Tag Example

In this example of the JSP declaration tag, we define the method that returns the cube of a given number and call it from the JSP expression tag. However, we can also use the JSP scriptlet tag to call the declared method.
<html>  
   <body>  
   <%!   
       int cube(int n){  
           return n * n * n;  
       }  
   %>  
   <%= "Cube of 3 is:"+cube(3) %>  
   </body>  
</html>  

Combining All Scripting Elements

JSP allows you to combine scripting elements to create dynamic content efficiently. Here is an example that combines declarations, scriptlets, and expressions:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Combined Example</title>
</head>
<body>
    <%
        int num1 = 5;
        int num2 = 3;
    %>
    <h1>Sum of <%= num1 %> and <%= num2 %> is: <%= sum(num1, num2) %></h1>
</body>
</html>

<%! 
    public int sum(int a, int b) {
        return a + b;
    }
%>

In this example:

  • A method sum is declared using a declaration element.
  • Two variables num1 and num2 are initialized using a scriptlet.
  • Expressions are used to display the values of num1, num2, and their sum.
In upcoming articles, we will explore these scripting elements further. In the next article, we will learn about the JSP expression tag with an example.

Comments