Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

JSP Implementation

The JSP implementation requires four files to implement this task:

  • Java file
  • Tag Library Descriptor
  • Web.XML configuration file
  • JSP file

A Java file implements the functionality of the custom tag. The Tag Library Descriptor file describes the structure of the custom tag and names the Java class file. The web.xml configuration file stores the location of the Tag Library Descriptor file. The JSP file represents a single web page that embeds the custom tag in HTML.

Java File: new_tag.java

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

public class new_tag extends javax.servlet.jsp.tagext.TagSupport
{
     private String message = "message not available";

     public int doStartTag() throws JspException
     {
          try
          {
               pageContext.getOut().print(message);
          }
          catch (Exception e)
          {
               throw new JspTagException(e.getMessage());
          }
          return SKIP_BODY;
     }

     public void setMessage(String text)
     {
          message = text;
     }
}

Tag Library Descriptor: example-taglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
     <tlibversion>1.0</tlibversion>
     <jspversion>1.1</jspversion>
     <shortname>new_tag</shortname>
     <info>Info about the new tag</info>
     <tag>
          <name>new_tag</name>
          <tagclass>new_tag</tagclass>
          <attribute>
              <name>message</name>
              <required>false</required>
          </attribute>
     </tag>
</taglib>

Configuration file: web.xml

<taglib>
     <taglib-uri>
          http://java.apache.org/tomcat/examples-taglib
     </taglib-uri>
     <taglib-location>
          /WEB-INF/jsp/example-taglib.tld
     </taglib-location>
</taglib>
<taglib>
     <taglib-uri>
          http://www.jsptest.com/taglib
     </taglib-uri>
     <taglib-location>
          /WEB-INF/taglib.tld
     </taglib-location>
</taglib>

JSP file: page_file.jsp

<%@ taglib uri="http://www.jsptest.com/taglib" prefix="ctl" %>

<HTML>
 <ctl:new_tag message="Creating a new function with JSP"></ctl:new_tag>
</HTML>
  • + Share This
  • 🔖 Save To Your Account