How Is Scripting Done in JSP?


Heather Bennett

Scripting in JSP (JavaServer Pages) is a powerful way to add dynamic functionality to your web applications. With JSP, you can easily embed Java code within HTML pages. In this article, we will explore how scripting is done in JSP and how it can enhance the functionality of your web applications.

Understanding Scripting Elements

JSP provides three types of scripting elements: scriptlets, expressions, and declarations. Let’s take a look at each of them.

1. Scriptlets: Scriptlets allow you to insert Java code directly into your JSP page.

They are enclosed within <% %> tags. For example:

<% int num1 = 5; int num2 = 10; int sum = num1 + num2; %>

2. Expressions: Expressions are used to insert the result of a Java expression directly into the output HTML stream.

They are enclosed within <%= %> tags. For example:


The sum of <%=num1%> and <%=num2%> is <%=sum%>.


3. Declarations: Declarations are used to declare variables or methods that can be accessed throughout the JSP page.

They are enclosed within <%! %> tags. For example:

<%! private String message = "Hello, World!"; public void printMessage() { System.out.println(message); } %>

Using Scripting Elements in JSP:

Now that we have an understanding of the different types of scripting elements, let’s see how we can use them effectively in our JSP pages.


Scriptlets are mainly used when you need to perform some complex operations or calculations. For example, you can use scriptlets to fetch data from a database, manipulate it, and display the results on your web page.


Let’s say we have a list of names stored in an ArrayList. We can use a scriptlet to iterate over the list and display each name.

<% ArrayList names = new ArrayList<>();

for (String name : names) {

<%= name %>

<% } %>

This scriptlet will generate the following HTML output:

  • John
  • Jane
  • Alice


Expressions are useful when you need to display the value of a variable or the result of an expression directly within your HTML content. Expressions make your code more concise and readable.


Suppose we have a variable called “username” that stores the current user’s name. We can use an expression to display a personalized greeting message.


Welcome, <%= username %>!


This expression will generate HTML output like this:

Welcome, John!


Declarations are helpful when you need to declare variables or methods that can be accessed throughout your JSP page. They are typically used for reusable code snippets.


Let’s say we want to store some common error messages in variables for easy access. We can use a declaration to define these variables.

<%! private String error1 = "Invalid username or password. "; private String error2 = "Email address is already taken. "; %>

Later in our JSP page, we can use these variables within scriptlets or expressions to display error messages.


Scripting in JSP is an essential aspect of building dynamic web applications. With scriptlets, expressions, and declarations, you can seamlessly incorporate Java code into your HTML pages.

Scripting elements provide flexibility and power to handle complex logic and data manipulation. By utilizing these scripting elements effectively, you can enhance the functionality and interactivity of your web applications.

So go ahead and start experimenting with scripting in JSP to take your web development skills to the next level!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy