| Refresh | Home EGTry.com

jsp custom tag implementation


use custom tag

<html>
<body>

<%@ taglib uri="/simple/myservlet-taglib" prefix="eg" %>

Radio stations that rock:

<ul>
  <eg:attr att1="98.5" att2="92.3" att3="107.7">
   <li><%= member %></li>
 </eg:foo>
</ul>

<eg:log>
  Did you see me on the stderr window?
</eg:log>

<eg:log toBrowser="true">
  Did you see me on the browser window as well?
</eg:log>

</body>
</html>


taglib

<?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>simpletag</short-name>

  <uri>http:/localhost:8080/simple/myservlet-taglib</uri>
  <description>
	A simple tab library for the examples
  </description>

  <tag>
    <name>ShowSource</name>
    <tag-class>myservlet.ShowSource</tag-class>
    <description> Display JSP sources </description>
    <attribute>
       <name>jspFile</name>
       <required>true</required>
       <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>       

  <!-- A simple Tag -->
  <!-- attr  tag -->
  <tag>
    <name>attr</name>
    <tag-class>myservlet.AttributeTag</tag-class>
    <tei-class>myservlet.AttributeTagExtraInfo</tei-class>
    <body-content>JSP</body-content>
    <description>
	Perform a server side action; uses 3 mandatory attributes
    </description>

    <attribute>
      <name>att1</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>att2</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>att3</name>
      <required>true</required>
    </attribute>
  </tag>


  <!-- Another simple tag -->
  <!-- log tag -->
  <tag>
    <name>log</name>
    <tag-class>myservlet.LogTag</tag-class>
    <body-content>TAGDEPENDENT</body-content>
    <description>
	Perform a server side action; Log the message.
    </description>
    <attribute>
	<name>toBrowser</name>
	<required>false</required>
    </attribute>
  </tag>


</taglib>


log tag java implementation

package myservlet;


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

import java.io.IOException;


/**
 * Log the contents of the body. Could be used to handle errors etc. 
 */
public class LogTag 
    extends ExampleTagBase
{
    boolean toBrowser = false;
    
    public void setToBrowser(String value) {
        if (value == null)
            toBrowser = false;
        else if (value.equalsIgnoreCase("true"))
            toBrowser = true;
        else
            toBrowser = false;
    }

    public int doStartTag() throws JspException {
        return EVAL_BODY_TAG;
    }
    
    public int doAfterBody() throws JspException {
        try {
            String s = bodyOut.getString();
            System.err.println(s);
            if (toBrowser)
                bodyOut.writeOut(bodyOut.getEnclosingWriter());
            return SKIP_BODY;
        } catch (IOException ex) {
            throw new JspTagException(ex.toString());
        }
    }
}

    
        
    


another custom tag implementation

package myservlet;


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

import java.io.*;

/**
 * Display the sources of the JSP file.
 */
public class ShowSource
    extends TagSupport
{
    String jspFile;
    
    public void setJspFile(String jspFile) {
        this.jspFile = jspFile;
    }

    public int doEndTag() throws JspException {
	if ((jspFile.indexOf( ".." ) >= 0) ||
            (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
            (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
	    throw new JspTagException("Invalid JSP file " + jspFile);

        InputStream in
            = pageContext.getServletContext().getResourceAsStream(jspFile);

        if (in == null)
            throw new JspTagException("Unable to find JSP file: "+jspFile);

        InputStreamReader reader = new InputStreamReader(in);
	JspWriter out = pageContext.getOut();


        try {
            out.println("<body>");
            out.println("<pre>");
            for(int ch = in.read(); ch != -1; ch = in.read())
                if (ch == '<')
                    out.print("<");
                else
                    out.print((char) ch);
            out.println("</pre>");
            out.println("</body>");
        } catch (IOException ex) {
            throw new JspTagException("IOException: "+ex.toString());
        }
        return super.doEndTag();
    }
}