JSP Declaration Tag


JavaServer Pages (JSP) is a technology used to create dynamic web content using Java. One of the key features of JSP is its ability to embed Java code directly into HTML pages through various scripting elements. In this blog post, we will explore the JSP declaration tag, its syntax, and its usage with examples.

What is the JSP Declaration Tag?

The JSP declaration tag is used to declare variables and methods that can be used within the JSP page. Unlike scriptlets, which are executed every time the page is requested, declarations are evaluated only once when the JSP page is translated into a servlet. This makes declarations useful for defining methods and variables that should be available throughout the JSP page.

Syntax

The JSP declaration tag is denoted by <%! ... %>, where the ellipsis (...) represents the variable or method declaration.

<%! declaration %>

How It Works

When the JSP page is processed, the declarations within the <%! ... %> tags are translated into instance variables and methods of the servlet class generated from the JSP page. These declarations are accessible throughout the JSP page, allowing for reusable code and methods.

Example Usage

Let's look at a few examples to understand how the JSP declaration tag works.

Example 1: Declaring Variables

In this example, we will declare some variables using the JSP declaration tag and use them in the HTML output.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP Declaration Tag Example</title>
</head>
<body>
    <%! 
        String firstName = "Ramesh";
        String lastName = "Fadatare";
        int age = 28;
    %>
    <p>First Name: <%= firstName %></p>
    <p>Last Name: <%= lastName %></p>
    <p>Age: <%= age %></p>
</body>
</html>

Example 2: Declaring Methods

This example demonstrates how to declare methods using the JSP declaration tag and call them within the JSP page.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP Declaration Tag Example</title>
</head>
<body>
    <%! 
        public String getFirstName() {
            return "Ramesh";
        }

        public String getLastName() {
            return "Fadatare";
        }
    %>
    <p>First Name: <%= getFirstName() %></p>
    <p>Last Name: <%= getLastName() %></p>
</body>
</html>

Complete Example with Screenshot

To demonstrate a complete example, let's create a JSP page that uses both variables and methods declared with the JSP declaration tag.

Complete JSP Example

Create a file named declaration.jsp with the following content:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.time.LocalDate" %>
<html>
<head>
    <title>JSP Declaration Tag Example</title>
</head>
<body>
    <h1>Using Variables and Methods in Declaration Tag</h1>

    <%! 
        String firstName = "Ramesh";
        String lastName = "Fadatare";
        int age = 28;
        LocalDate dob = LocalDate.of(1991, 3, 24);

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }
    %>

    <p>First Name: <%= getFirstName() %></p>
    <p>Last Name: <%= getLastName() %></p>
    <p>Age: <%= age %></p>
    <p>Date of Birth: <%= dob %></p>
</body>
</html>

Output Screenshot

When you run this JSP page in a web browser, you will see the following output:

JSP Declaration Example Output

Best Practices

While the JSP declaration tag is useful, it is generally recommended to minimize the amount of Java code in JSP pages for better maintainability. Consider using JavaBeans, custom tags, or expression language (EL) to separate business logic from presentation.

  • Minimize the number of declarations in a JSP.
  • Avoid dumping large amounts of code in a JSP.
  • Refactor logic into separate Java classes and use the Model-View-Controller (MVC) pattern.

In the next article, we will learn about JSP implicit objects with an example.


For more detailed tutorials and examples on JSP, check out the JSP Tutorial series.

Comments