In this JavaEE JSP Video, we will see how to use SimpleTagSupport class to override the doTag() method. The doTag will do the Job of HTML Tag rendering.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package tube.codingexamples.jtags; import java.io.IOException; import java.util.ArrayList; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; import tube.codingexamples.bean.Title; public class ListBookTitlesTag extends SimpleTagSupport { //Sample 01: Define the books as a collection ArrayList books; //Sample 02: Set the book titles public void setBooks(ArrayList books) { this.books = books; } //Sample 03: Do the Html Tag to represent the Books Titles @Override public void doTag() throws JspException, IOException { //3.1: Get output stream for writing html content JspContext jc = getJspContext(); JspWriter out = jc.getOut(); //3.2: Iterate the Book collection & form Html for(Object obj : books) { Title BookTitle = (Title) obj; out.println("<p>"); out.println(BookTitle.getBookTitle()); out.println("</p>"); out.println("<p>"); out.println(BookTitle.getType() + ", "); out.println(BookTitle.getBookPrice()); out.println("</p>"); } } } |
Categories: JSP