Table of contents
1.
Introduction
1.1.
Custom attributes in JSP
2.
Iterators with a custom tag
3.
 
4.
 
5.
 
6.
Frequently Asked Questions
7.
Key takeaway
Last Updated: Mar 27, 2024

JSP Attributes and Iteration

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Attributes and iteration in JSP are very useful in dynamically viewing servlets. We will first look at how to set up custom attributes in JSP, then will move on with examples for better understanding. Iterators will be coupled with custom attributes in JSP for another illustration.

Custom attributes in JSP

We can define as many custom attributes in JSP as per our need. We will follow these points to create an attribute.

First, we will define the property in the tag handler class with the attribute name, then define its setter method.

Then, we will define the attribute element inside the TLD(Tag Library Descriptor) file's tag element.

The syntax to define the tag handler is as below:

<m:square number=”2”></m:square>

 

Let us see one example illustration of a JSP custom tag.

index.jsp file

<%@ taglib uri="tags.tld" prefix="m" %> 
Square of 4 is: <m:Square num="4"></m:Square>


SquareNumber.java

package taghandler; 
import javax.servlet.jsp.JspException; 
import javax.servlet.jsp.JspWriter; 
import javax.servlet.jsp.tagext.TagSupport; 
 public class Squarenum extends TagSupport{ 
private int num; 
    
public void setnum(int num) { 
   this.num = num; 
} 
 public int doStartTag() throws JspException { 
   JspWriter out=pageContext.getOut(); 
   try{ 
       out.print(num*num); 
   }catch(Exception e){e.printStackTrace();} 
    
   return SKIP_BODY; 
} 
} 
You can also try this code with Online Java Compiler
Run Code

 

tags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE taglib 
       PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
       "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> 
 <taglib> 
 <tlib-version>1.0</tlib-version> 
 <jsp-version>1.2</jsp-version> 
  <description>A simple tab library for the examples</description> 
  <tag> 
   <name>Square</name> 
   <tag-class>taghandler.Squarenum</tag-class> 
   <attribute> 
   <name>num</name> 
   <required>true</required> 
   </attribute> 
 </tag> 
</taglib>

Output:


You can also read about the JSP Architecture and Lifecycle.

Iterators with a custom tag

Iterating the content of a tag can be done using the doAfterBody() method of the iterationTag interface. With the tagSupport class's help, iteration can be implemented using the EVAL_BODY_AGAIN constant in the doAfterBody() function.

Let us have a look at one example illustration implementing iterators with a custom tag.

<%@ taglib uri="tags.tld" prefix="m" %> 
 2 ^ 3 = <m:power num="2" power="3"> 
body 
</m:power>

tags.tlb

<taglib> 
   <tlib-version>1.0</tlib-version>  
   <uri>http://tomcat.apache.org/example-taglib</uri> 
  
   <tag> 
     <name>power</name> 
     <tag-class>taghandler.Powernum</tag-class> 
      
     <attribute> 
     <name>num</name> 
     <required>true</required> 
     </attribute> 
      
     <attribute> 
     <name>power</name> 
     <required>true</required> 
     </attribute> 
    
   </tag> 
 </taglib> 

Output 

 

 

 

 

 

You can also read about What is Servlet in detail.

Frequently Asked Questions

1. What is doStartTag and doEndTag?

 Ans: The JSP page computed this tag handler when the start tag comes across. While doEndTag is called when the JSP's page servlet evaluates the end tag.

2. What is the doAfterBody tag?

Ans: It helps evaluate the body if certain conditions are met on purpose. Our example counter is incremented each time until we get to a specified value.

3. What are the attributes?

Ans: Attribute tag in JSP is used to define the value of a tag attribute or property in the body of XML document.jsp: attribute element accepts a name and a trim attribute. The trim attribute is optional.

4. What are TLD files?

Ans: TLD stands for tag library descriptor files used to validate tags. It is used to define the names, tag class used and attributes used mentioned in the tag wherever used.

5. Describe javax.servlet.jsp package.

Ans: This package has classes and interfaces to describe and define the contracts between the JSP page class and the runtime environment for the instance of that particular class.

Key takeaway

Congratulations on getting through the article on custom attributes in JSP and iteration in JSP! You got to know how to set up custom attributes in JSP. Illustrations will get you to a level of understanding on using them. We also saw iterators being used in custom attributes in JSP.

If you want to get started with JSP, you can check out the JSP Syntax and First App article.

Happy Learning!

Live masterclass