Logo Tag : Tag « JSP « Java






Logo Tag

package com.java2s;


///Custom tag
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;

/**
 * This tag generates a thumbnail image using HTML img tag, next to text
 * message. The user specifies the content of the message and the Heading level
 * (i.e.,
 * <H1>-
 * <H2>)
 */

public class LogoTag extends BodyTagSupport implements TryCatchFinally {

  private String heading = null;

  private String image = null;

  //stamp.gif, 42 x 54
  private String width = null;

  private String height = null;

  public int doStartTag() throws JspException {

    //this method assumes that attribute properties have been set.
    try {

      int h = new Integer(heading).intValue();

      if (!(h > 0 && h < 7))
        throw new JspException(
            "The 'heading' attribute value must between 1 and 6 inclusive.");

    } catch (Exception e) {
      throw new JspException(e.getMessage());
    }

    return EVAL_BODY_BUFFERED;

  }

  public int doEndTag() throws JspException {

    JspWriter out = pageContext.getOut();
    String imgDir = ((HttpServletRequest) pageContext.getRequest())
        .getContextPath()
        + "/images/";
    String message = getBodyContent().getString().trim();
    try {
      out.println(new StringBuffer("<img src=\"").append(imgDir).append(
          image).append("\" width=\"").append(width).append(
          "\" height=\"").append(height).append("\" align=\"left\">")
          .append("<H").append(heading).append(">").append(message)
          .append("</H").append(heading).append(">").toString());

    } catch (java.io.IOException io) {
    }

    return EVAL_PAGE;
  }

  public void doCatch(Throwable t) {

    try {

      pageContext.getOut().println(t.getMessage() + "<br />");

    } catch (java.io.IOException io) {
    }
  }

  public void doFinally() {

    //do nothing here, since we don't have any resources open
    //like database connections

  }

  public void setHeading(String level) {

    this.heading = level;

  }

  public void setImage(String name) {

    this.image = name;

  }

  public void setWidth(String width) {

    this.width = width;

  }

  public void setHeight(String height) {

    this.height = height;

  }

  public void release() {

    heading = null;
    image = null;
    width = null;
    height = null;

  }

}
//
package com.java2s;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * This tag generates a thumbnail image using HTML img tag, next to text
 * message. The user specifies the content of the message and the Heading level
 * (i.e.,
 * <H1>-
 * <H2>)
 */

public class SimpleLogoTag extends SimpleTagSupport {

  private String heading = null;

  private String image = null;

  private String width = null;

  private String height = null;

  public void doTag() throws JspException, IOException {

    JspContext jspContext = getJspContext();

    //this method assumes that attribute properties have been set.
    try {

      int h = new Integer(heading).intValue();

      if (!(h > 0 && h < 7))
        throw new JspException(
            "The 'heading' attribute value must between 1 and 6 inclusive.");

    } catch (Exception e) {
      throw new JspException(e.getMessage());
    }

    JspWriter out = jspContext.getOut();

    String imgDir = (String) jspContext.findAttribute("imgDir");

    if (imgDir == null || "".equals(imgDir))
      throw new JspException(
          "No attribute provided specifying the application's image directory.");

    out.println(new StringBuffer("<img src=\"").append(imgDir)
        .append(image).append("\" width=\"").append(width).append(
            "\" height=\"").append(height).append(
            "\" align=\"left\">").append("<H").append(heading)
        .append(">").toString());
    getJspBody().invoke(null);
    out.println(new StringBuffer("</H").append(heading).append(">")
        .toString());

  }

  public void setHeading(String level) {

    this.heading = level;

  }

  public void setImage(String name) {

    this.image = name;

  }

  public void setWidth(String width) {

    this.width = width;

  }

  public void setHeight(String height) {

    this.height = height;

  }

}
//logo.tag
<%@ tag body-content="scriptless" description="Writes the HTML code for inserting a logo." %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ attribute name="heading" required="true" rtexprvalue=
  "true" description="The heading level for the logo."%>

<%@ attribute name="image" required="true" rtexprvalue=
  "true" description="The image name for the logo."%>

<%@ attribute name="width" required="true" rtexprvalue=
  "true" description="The image width for the logo."%>

<%@ attribute name="height" required="true" rtexprvalue=
  "true" description="The image height for the logo."%>
 
<img src="${imgDir}${image}" width=
  "${width}" height="${height}" align="left">

<H${heading}>
  <jsp:doBody/></H${heading}>

//logoTest.jsp
<%@ taglib uri="java2s.com.tags" prefix="cbck" %>
<html>
<head><title></title></head>
<body>
<% session.setAttribute("imgDir",(request.getContextPath() + "/images/")) %>
<cbck:logo heading="2" image="stamp.gif" width="42" height="54">Thanks for visiting</cbck:logo>

Here's all the other stuff this page contains...
</body>
</html>

//myTag.tld


<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">

<tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>cbck</short-name>
    <uri>java2s.com.tags</uri>
    <description>Cookbook custom tags</description>
  
  <listener>
  <listener-class>com.java2s.ReqListener</listener-class>
    </listener>  

<tag>
        <name>logo</name>
        <tag-class>com.java2s.LogoTag</tag-class>
        <body-content>scriptless</body-content>
        <description>This tag writes a logo inside the JSP.</description>
        <attribute>
            <name>heading</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <description>The heading level for the logo; 1 through 6.</description>
        </attribute>
        
        <attribute>
            <name>image</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <description>The image name for the logo.</description>
        </attribute>
    
     <attribute>
            <name>width</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <description>The image width for the logo.</description>
        </attribute>
    
     <attribute>
            <name>height</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <description>The image height for the logo.</description>
        </attribute>
    </tag>

</taglib>

           
       








Related examples in the same category

1.Your own simple JSP tag
2.Create your own tag: a custom tag body
3.A custom tag that has neither attributes nor body content.
4.A custom tag: empty with attributes
5.A custom tag: scripting variable
6.Write your own tag
7.A custom tag: empty
8.Tag lifecycle with Attribute
9.A custom tag: iteration
10.JSP Simple Tags
11.JSP tag: advanced tagsJSP tag: advanced tags
12.JSP classic tagsJSP classic tags
13.JSP Tag Libraries and JSTLJSP Tag Libraries and JSTL
14.JSP Directives: HTML tag