Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Encapsulate reusable functionality in JSP tags

Build your own custom JSP tag with Tomcat

  • Print
  • Feedback

Page 2 of 3

 
package examples;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
 * Given a URI, uses the servlet context to find out how large the
 * "real" file is in bytes.
 *
 * @author      Simon Brown
 */
public class SizeTag extends javax.servlet.jsp.tagext.TagSupport {
        /** the URI of the file */
        private String uri;                       
        /**
         * Performs the processing of this tag.
         */
        public int doStartTag() throws JspException {
            StringBuffer html = new StringBuffer();
            // ask the container (via the servlet context) for the
            // real path of a file pointed to by a URI
            String realPath = pageContext.getServletContext().getRealPath(uri);
            // we can now find out how large the file is
            File f = new File(realPath);
            long fileLength = f.length();
            // build up the HTML piece by piece ...
            html.append(fileLength);
            html.append(" bytes");
                       
            // ... and write it
            try {
                pageContext.getOut().write(html.toString());
            } catch (IOException ioe) {
                throw new JspException(ioe.getMessage());
            }
            return EVAL_BODY_INCLUDE;
        }
        /**
         * Standard JavaBeans style property setter for the URI.
         *
         * @param s        a String representing the URI
         */
        public void setUri(String s) {
            this.uri = s;
        }
}                       


You may have noticed that instead of implementing the Tag interface, I'm actually extending a class called TagSupport. That is provided as part of the standard Servlet/JSP distribution and contains a default implementation of the methods defined in the Tag interface.

There are only two methods of interest in this tag, doStartTag() and setUri(). When I defined the tag in the tag library descriptor, I said that it would have an attribute called uri. Attribute values are passed to tags via the appropriate set method. The Java code that performs that is generated by the JSP engine, which uses Java's reflection mechanism to check that the appropriate method does exist on the tag. For that reason, tags must define a set method for each of its defined attributes. The name of the method should follow the JavaBeans convention of changing the first character of the property name to uppercase. The setUri() method is defined as taking a String, although you can use any object.

Custom tags on a JSP use the XML notion of an opening and closing tag, reflected by the doStartTag() and doEndTag() methods defined in the Tag interface. As my tag is a very simple example, I've chosen only to implement doStartTag(), which gets called when the JSP engine encounters the opening (or starting) tag. My implementation uses the tag's page context to find out a file's local location, to which the specified URI points. Once the tag has that information, it uses a standard java.io.File object to work out the file's length and generates a small, dynamic HTML string. That generated HTML is then sent back to the JSP engine, using the output stream associated with the page context. The return value tells the JSP engine how the tag processing should continue. In that case, you're telling the engine to continue as normal and evaluate any body content.

  • Print
  • Feedback

Resources