Showing posts with label custom tag. Show all posts
Showing posts with label custom tag. Show all posts

Thursday, 6 December 2012

JSP custom tag with body

 Custom tag JSP

Steps for creating custom tag get from here

code for creating jsp custom tag with body

Tag implementation


TagBody.java

package st;

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


public class TagBody extends TagSupport
{

     public int doStartTag() throws JspException
     {
        try{
               JspWriter out = pageContext.getOut();
               out.println( "<table border=1>");
           
                      out.println("<tr><td>  ");
                  

            }
        catch(Exception ex)
        {
        }
        return EVAL_BODY_INCLUDE;
     }

    public int doEndTag() throws JspException

Custom tag using JSP without body

JSP custom tag without body

steps

first create a folder named as st in WEB-INF\classes directory

Then create java file named as HellTag.java  inside st

paste the following on  HellTag.java file

package st;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class HellTag extends TagSupport
{
    public int doStartTag()
    {
        try{
            JspWriter js=pageContext.getOut();
            js.print("Hello");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return SKIP_BODY;
    }
}

compile the HelloTag.java file by following manner
javac HellTag.java -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar"

Then create st.tld file inside the WEB-INF directory