JSP Declaration Tag Syntax with Example

The JSP declaration tag is used to declare fields and methods.

The code written inside the JSP declaration tag is placed outside the service() method of the auto-generated servlet.
All JSP Examples at JSP Source Code Examples

The Syntax of JSP Declaration Tag

<%!  Declaration %>

JSP Declaration Tag Example

Let's demonstrates how to use JSP declaration tag to declare fields and methods with an example:
<%@page import="java.time.LocalDate"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Using variables in declaration tag</h1>
    <%!String firstName = "Ramesh";%>
    <%!String lastName = "Fadatare";%>
    <%!int age = 28;%>
    <%!LocalDate dob = LocalDate.of(1991, 03, 24);%>

    <br> First Name:
    <%=firstName%>
    <br> Last Name:
    <%=lastName%>

    <br> Age:
    <%=age%>

    <br> Date of Birth:
   <%=dob%>

    <h1>Using methods in declaration tag</h1>
    <%! 
        String getFirstName(){
            return "Ramesh";
        }
    %>

    <%! 
       String getLastName(){
          return "Fadatare";
       }
    %>


    <br> First Name:
    <%= getFirstName() %>

    <br> Last Name:
    <%= getLastName() %>
</body>
</html>
Let's invoke this JSP page from a Web browser, you see the table on a browser:
All JSP Examples at JSP Source Code Examples

Comments