Followers

JSP:Custom Tags (TagSupport2)

index.jsp
=========
<%@ taglib uri="WEB-INF/mytags.tld" prefix="mytags" %>
<mytags:hello>
    I am Mohan  
</mytags:hello>
<br ><br >
<mytags:adjust>
    Welcome to this application.  
</mytags:adjust>  
    <br ><br >
    <b>
        You requested this application at
    </b>
    <br ><br >
    <mytags:time/>
    <br ><br >
    <mytags:adjust>
        Have a nice day
        <br ><br >
        BYE, BYE
    </mytags:adjust>
        <br ><br >
        Try again
        <br ><br >
        <mytags:time/>
    <br ><br >
    <mytags:hello />
   
   
mytags.tld
===========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/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>hello</name>
    <tag-class>com.java9m.mohan.HelloTag</tag-class>
  </tag>
  <tag>
    <name>adjust</name>
    <tag-class>com.java9m.mohan.AdjustTag</tag-class>
  </tag>
    <tag>
    <name>time</name>
    <tag-class>com.java9m.mohan.TimeTag</tag-class>
  </tag>
 
 
</taglib>

HelloTag.java
==============

package com.java9m.mohan;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloTag extends TagSupport{
 
public int doStartTag() {
     try{
    JspWriter out=pageContext.getOut();//returns the instance of JspWriter  
     out.print("<center><b>");
     out.println("Hello client how are you?");
     out.println("<br ><br >");
     out.println("</b></center>");
    }catch(Exception e){System.out.println(e);}
    return EVAL_BODY_INCLUDE;//will not evaluate the body content of the tag
}
}

AdjustTag.java
===============
package com.java9m.mohan;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class AdjustTag extends TagSupport{
 
public int doStartTag() throws JspException {
     try{
    JspWriter out=pageContext.getOut();//returns the instance of JspWriter  
     out.print("<center><b>");//printing date and time using JspWriter
     out.println("?????????????????");
     out.println("<br ><br >");
     out.println("******************");
     out.println("</b></center>");
    }catch(Exception e){System.out.println(e);}
    return EVAL_BODY_INCLUDE;//will not evaluate the body content of the tag
}
public int doEndTag(){
    try {
        JspWriter out=pageContext.getOut();
        out.print("<center><b>");//printing date and time using JspWriter
        out.println("==================");
        out.println("<br ><br >");
        out.println("+++++++++++++++++++");
        out.println("</b></center>");
    } catch(Exception e){System.out.println(e);}
return EVAL_PAGE;

}


}

TimeTag.java
=============
package com.java9m.mohan;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import static javax.servlet.jsp.tagext.Tag.EVAL_PAGE;
import static javax.servlet.jsp.tagext.Tag.SKIP_BODY;
import javax.servlet.jsp.tagext.TagSupport;
public class TimeTag extends TagSupport{
 
public int doStartTag() throws JspException {
     try{
         Date d = new Date();
    JspWriter out=pageContext.getOut();//returns the instance of JspWriter  
     out.print("<center><b>");
     out.println("Now the time is");
     out.println("<br ><br >");
     out.println("Hours "+d.getHours());
     out.println("<br ><br >");
     out.println("Minutes "+d.getMinutes());
     out.println("<br ><br >");
     out.println("Seconds "+d.getSeconds());
     out.println("</b></center>");
    }catch(Exception e){System.out.println(e);}
    return SKIP_BODY;//will not evaluate the body content of the tag
}
public int doEndTag(){
    try {
        JspWriter out=pageContext.getOut();
        out.println("Thank you");
        out.println("<br ><br >");
        out.println("I hope you remember this time");
        out.println("<br ><br >");
        out.println("BYE, BYE");
    } catch(Exception e){System.out.println(e);}
return EVAL_PAGE;

}


}

No comments:

Post a Comment