Scripting elements Scriptlet, Expression and Declaration tags in JSP

What are Scripting elements in JSP? How to use Scripting elements Scriptlet, Expression, and Declaration tags in JSP?

Scripting elements in JSP?

Scripting elements are JSP tags, that are used to write the Java code inside a JSP file. As we discussed in the previous tutorial of the JSP life cycle. JSP is a Java server page, that allows writing java code with HTML code in a single file with that we can write HTML code.

Java code is written inside this <% java code.... %> tag and at the time of translation, this java code is processed by the JSP engine, and the remaining HTML, CSS, and JS code will be run directly on the browser.

Following are JSP Scripting elements

  • Scriptlet tag in JSP.
  • Expression tag in JSP.
  • Declaration tag in JSP.

Scriptlet tag in JSP

The Scriptlet tag is used to write Java code inside the JSP file. we will write java code inside <% %> this tag Let’s see an example of the Scriptlet tag in JSP.

<html>  
<body>  
<% 
out.print("welcome to CodeBun.com"); 
%>  
</body>  
</html>

Expression tag in JSP

An Expression tag is used to display variable values or print a method. We don’t need to write “out.println”.

We can perform Arithmetic or logical operation inside the Expression tag in JSP. Syntax : <%= expression%>

<html>
    <head>
        <title>Expression JSP</title>
    </head>
   <%
       int x = 20 ;
   %>
  <body>
        Page Count is  <%= x+5 %>   
  </body>

Declaration tag in JSP

Declaration tag is used to declare a value to the variable but at the same time we can declare value inside the scriptlet tag also but the difference is, At the time of translation the statement of declaration tag will come inside the servlet but outside the service method. And the service method calls on every request so it’s good if you declare all the variables inside the declaration tag.

Syntax is : <%! int a =5; %>

<html>
    <head>
        <title>Declaration tag in jsp</title>
    </head>
   <%!
       int x = 20;
   %>
  <body>
          <% out.println(++x); %>   
  </body>
</html>

Some Importent point realted JSP Scripting elements:

  • Java code which is written in  <% %> tag moves in _jspService() method.
  • Java Code which is written is <%! %> moves outside the _jspService() method.
  • We can call any predefined or user-defined method inside the Expression tag <%= %>.

Now, let us look at the examples of each tag discussed above:

Example of Scriptlet Tag in JSP

In this example, we will use the JSP scriptlet tag to print the current date and time. We can use the simple Date object. You can use any API for DateAndTime.

Step 1: Open Eclipse IDE > File > New > Dynamic Web Project > Enter Project Name (ScriptletTagExample) > Next > Next > Finish.

Step 2: Go to project name > expand it > right-click on Web content > click New > JSP file > enter the name of the JSP file(dateandtime.jsp) > Next > Finish.

Step 3: Now, write the following code, here we can see the java code is written inside this <%%> tag:

<%@page import="java.sql.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Use of Scriptlet Tag</title>
</head>
<body style="background: linear-gradient(145deg,#12c2e9,#c471ed)">
<div>
 <table>
  <tr>
   <td>
    <h3>Current Date & Time</h3>
   </td>
  </tr>
  <tr>
   <td>
    <%java.util.Date date = new java.util.Date();
      out.print(date.toLocaleString());
    %>
   </td>
  </tr>
 </table>
</div>
</body>
</html>

Step 4: Run it on the Server and see the following output

Example of Expression Tag in JSP

In this example, we will calculate the product of two numbers and we will display the result using the Expression tag in JSP.

Step 1: Open Eclipse IDE > File > New > Dynamic Web Project > Enter Project Name (ExpressionTagExample) > Next > Next > Finish.

Step 2: Go to project name > expand it > right-click on Web content > click New > JSP file > enter the name of the JSP file(product.jsp) > Next > Finish.

Step 3: Now, write the following code, here we can display the result using this <%= %> tag:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <%out.println("Calculate Product :"); %>
 <%int a = 10;
   int b = 20;
   int c;
   c = a * b;
 %>
 <%=c %>
</body>
</html>

Step 4: Run it on the Server and see the following output

Example of Declaration Tag in JSP

In this example, we will calculate a square of a number inside the declaration Tag and will display the result using the expression tag in JSP.

Step 1: Open Eclipse IDE > File > New > Dynamic Web Project > Enter Project Name (DeclarationTagExample) > Next > Next > Finish.

Step 2: Go to project name > expand it > right-click on Web content > click New > JSP file > enter the name of the JSP file(square.jsp) > Next > Finish.

Step 3: Now, write the following code, here we can display the result using this <%! %> tag:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <%!
  int square(int n){
  return n*n;
 }
 %>
 <%= "Square is :"+square(5)%>
</body>
</html>

Step 4: Run it on the Server and see the following output

Thus, this was all about Scripting Elements in JSP. If you want to know more about JSP Tags do check this article https://codedec.com/tutorials/what-are-jsp-scripting-elements/

Practise Task in JSP