declaration tag of jsp

In this post, you will learn:

  • What is declaration tag in JSP?
  • What is the role of declaration tag in the auto generated servlet?
  • How to override jspInit() method with the help of declaration tag?

The declaration tag of JSP:

declaration tag in JSP facilitate data members and methods definition in the auto generated servlet. Main use of this tag is the overriding of jspInit() and jspDestroy() methods. You have been told that for each JSP a servlet is auto generated at the time of translation. If this class was defined by us then these life cycle methods could have been directly overridden. It is generated by the translator hence we are provided this tag. Through it we tell the translator that define these methods in the class on our behalf. It has following syntax:

<%! data members and methods definition %>

At the time of translation, contents of the declaration tags are copied as the member of the auto generated servlet as demonstrated by the following diagram:

declaration_tag_jsp

In this tag, the implicit objects can’t be used because they are declared in the _jspService() method in which scriptlet contents are copied. In the following example, I have used the declaration tag to override jspInit() life cycle method of JSP, and to define a data member & additional method.

Practical example of the declaration tag:

First, the index.html :

<form method="post" action="hello.jsp">
Name <input type="text" name="name"><br/>
<input type="submit" value="submit">
</form>

Second, the hello.jsp :

<!-- Declaration tag starts. -->
<%!
/* A data member is defined. */
private String userName;

/* An additional method is defined. */
public String sayHello()
{
return "Hello, "+userName;
}

/* jspInit() method is overridden. */
public void jspInit()
{
System.out.println("hello.jsp is initialized.");
}
%><!-- Declaration tag ends. -->

<!-- Scriptlet tag starts. -->
<%
/* Value of the data member is set using the request parameter. */
userName=request.getParameter("name");

/* Additional method is invoked and the value returned by it is sent as response. */
out.println(sayHello());
%>
<!-- Scriptlet tag ends. -->

When the first request is sent to the hello.jsp, the message of jspInit() method is displayed on the server’s console. In the subsequent requests, this message isn’t displayed because initialization is performed only once.


Its time to recap the main concepts:

  1. declaration tag is used to get the data members and method defined in the auto generated servlet.
  2. Contents of the declaration tag are made the member of the auto generated servlet.
  3. Implicit objects of the scriptlet can’t be used in the declaration tag.
  4. Main use of the declaration tag is the overriding of jspinit() and jspDestroy() life cycle methods.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “declaration tag of jsp

Leave a Reply

Your email address will not be published. Required fields are marked *