JSP

JSP Scripting Elements

Scripting elements are important part of JSP that makes page dynamic. Scripting elements can be classified in 3 categories:

1. Declaration Tags.
2. Expression Tags.
3. Scriptles.

Declaration Tags: Declaration Tags is used to define variables and methods in JSP. Declaration Tags do not produce any output. Declaration Tag starts with <%! and ends with %>
Syntax.<%! datatype variable = value; %>

<body>
  <%!
  int age = 25;
  String name = "Dinesh";
  %>
  </body>

Expression Tags: Unlike Declaration Tags, Expression Tags produce output that can be used to display result on JSP. Expression Tag starts with <%= and ends with %>
Syntax- <%=variable %>
Examaple :

<body>
  <%=name %>
 <%=age%>
  </body>

Scriptlets: Scriptlets are useful when you want to include JAVA code inside JSP. You may want to create a function that can be used to generate output based on different parameters passed at run time. In this case you may want to create JAVA function in Scriptlets and than call this function in Expression Tags at multiple location in JSP to display result on browser. Scriptlets starts with <% and ends with %>.
Syntax-<% out.println(“www.dineshonjava.com”) %>
Examaple :

<body>
  <%
    Date dob = new Date();
    out.println("Date of birth is "+dob);
    out.println("www.dineshonjava.com");
  %>
  </body>

Complete example here
index.jsp

<%@ page import="java.util.Date" %>
<html>  
    <body>
 <h1> Declaration  </h1>
 <%!
  int age = 25;
  String name = "Dinesh";
 %>
  <h1>Expression </h1>
 <%=name %>
 <%=age%>
 <h1>Scriptlets</h1>
    <%
  Date dob = new Date();
  out.println("Date of birth is "+dob);
  out.println("www.dineshonjava.com");
 %>
 </body>  
 </html>  

Copy the index.jsp file in to the webapp directory of tomcat.
C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsindex

and start the server and hit the following url-
http://localhost:8080/index/index.jsp

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago