What Are the Different Types of Scripting Elements Present in JSP?


Angela Bailey

Scripting elements in JSP (JavaServer Pages) allow us to embed Java code within an HTML page. These elements enable us to dynamically generate content, interact with databases, perform calculations, and execute various other tasks. There are three main types of scripting elements available in JSP:

1. Scriptlet Tags:

Scriptlet tags are enclosed within <% %> delimiters. They allow us to write Java code directly inside the JSP file.

Any code placed between these delimiters will be executed when the JSP page is accessed by a client.

Here’s an example of a scriptlet tag:

    String message = "Hello, World!";

2. Declaration Tags:

Declaration tags are enclosed within <%! %> delimiters.

They are used to define methods, variables, and classes that can be accessed throughout the entire JSP page. The code written within these tags is placed outside the service() method but still inside the generated servlet class.

Here’s an example of a declaration tag:

    private int calculateSum(int num1, int num2) {
        return num1 + num2;

3. Expression Tags:

Expression tags are enclosed within <%= %> delimiters. They allow us to evaluate and print the result of a Java expression directly into the JSP output.

The evaluated expression is converted to a String and then displayed in the HTML page.

Here’s an example of an expression tag:

Welcome, <%= request.getParameter("username") %>


In summary, JSP provides three types of scripting elements: scriptlet tags (<% %>) for embedding Java code, declaration tags (<%! %>) for defining methods and variables, and expression tags (<%= %>) for evaluating and printing Java expressions in the output.

These elements give us the flexibility to create dynamic web pages with ease.

It’s important to note that excessive use of scripting elements can make the code harder to maintain and debug. Therefore, it’s recommended to follow best practices and separate business logic from presentation as much as possible.

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

Privacy Policy