🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
The scripting elements provide the ability to insert Java code inside the JSP.
- JSP expressions
- JSP scriptlets
- JSP declarations
1. JSP expressions
The Syntax of JSP Expression Tag
<%= expression %>
JSP Expression Tag Examples
<html>
<body>
Converting a string to uppercase:
<%=new String("Hello World").toUpperCase()%>
</body>
</html>
2. JSP scriptlets
<% statement; [statement; …] %>
Example of JSP scriptlet tag
In this example, we are displaying a HelloWorld message.
```jsp
<html>
<body>
<% out.print("HelloWorld"); %>
</body>
</html>
3. JSP declarations
The Syntax of the JSP Declaration Tag
<%! Declaration %>
JSP Declaration Tag Example
<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
sumis declared using a declaration element. - Two variables
num1andnum2are initialized using a scriptlet. - Expressions are used to display the values of
num1,num2, and their sum.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment