Write your own tag : Tag « JSP « Java






Write your own tag

// Sample tld file
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
   "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

    <!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>Mark's Simple Tags</short-name>

  <tag>
    <name>HelloWorldTag</name>
    <tag-class>test.HelloWorldTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

// Sample JSP file

<%@ taglib uri='webapp/META-INF/simple.tld' prefix='hw' %>

<html>
  <body>
    <hw:HelloWorldTag />
  </body>
</html>

// Sample Java file

package test;

import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

public class HelloWorldTag extends TagSupport
{
    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            out.write("Hello world!");
        }
        catch(Exception e)
        {   
            throw new JspException(e.getMessage());
        }
        return EVAL_PAGE;
    }
}


           
       








Related examples in the same category

1.Your own simple JSP tag
2.Create your own tag: a custom tag body
3.A custom tag that has neither attributes nor body content.
4.A custom tag: empty with attributes
5.A custom tag: scripting variable
6.Logo Tag
7.A custom tag: empty
8.Tag lifecycle with Attribute
9.A custom tag: iteration
10.JSP Simple Tags
11.JSP tag: advanced tagsJSP tag: advanced tags
12.JSP classic tagsJSP classic tags
13.JSP Tag Libraries and JSTLJSP Tag Libraries and JSTL
14.JSP Directives: HTML tag