Thursday, 4 October 2012

JSP SimpleTagSupport Example


JSP SimpleTagSupport Example

The SimpleTagSupport class is a utility class intended to be used as the base class for new simple tag handlers. The SimpleTagSupport class implements the SimpleTag interface and adds additional convenience methods including getter methods for the properties in SimpleTag. I am going to follow below steps to create a simple JSP Tag

1. Create Tag handler class

package com.pretech;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class PretechSimpleTag extends SimpleTagSupport {
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        out.print("Hello world From tag library");


2.Create Tld file and placed in WEB-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns=""
xsi:schemaLocation="" version="2.0">
<description>Hello world</description>

3. Create Jsp file with taglib directive and tag (PretechTag.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  <%@ taglib uri="/WEB-INF/pretechtld.tld"
 prefix="pretech" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          Pretech Tag Library example

4. Deploy application and run it Output




Anonymous said...

Great example but I want to see more examples about
how to create taglibs without use code java.

SimpleTag is like a Servlet Of course not work same ! I need somothing more better