Logo Tag : Tag « JSP « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » JSP » TagScreenshots 
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 > && 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 = ((HttpServletRequestpageContext.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 > && 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 = (StringjspContext.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; 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.