Followers

JSP:Custom Tags (TagSupport1)

index.jsp
=========
<%@ taglib uri="WEB-INF/mytags.tld" prefix="mytags" %>
Current Date and Time is: <mytags:today>
<br ><br>
HI, How are you?
</mytags:today>

mytags.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>
  <short-name>simple</short-name>  
<tag>
<name>today</name>
<tag-class>com.java9m.mohan.MyTagHandler</tag-class>
</tag>
</taglib>

MyTagHandler.java
=================

package com.java9m.mohan;
import java.util.Calendar;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class MyTagHandler extends TagSupport{
 
public int doStartTag(){
   
    JspWriter out=pageContext.getOut();//returns the instance of JspWriter
    try{
     out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter
    }catch(Exception e){System.out.println(e);}
    return SKIP_BODY;//will not evaluate the body content of the tag
}
}

No comments:

Post a Comment