caucho
Resin
FAQ
Reference Guide
Demo
Tutorial

JSP page
Config
URLs
Database Forms
XTP Copy
Hello Tag
Vary Filter
HardCore
Mailing Forms
Beans
Cache
XSL Filter
run-at
jndi app config

Attr Tag
Mail Tag
XSL Tag
Connection Tag
 Mail Tag

Attr Tag
Hello Tag
XSL Tag

Body tags can grab JSP results and send them to an entirely different location. It's easy to create a custom tag that sends part of a JSP to a survey account. The required attribute to will specify the mail address for the mail and subject specifies the subject. The enclosed JSP contains the body of the mail.

The contents of the tag can be any JSP code. You can still use arbitrary scriptlets writing to out.

Using the mail tag

This example use of the mail tag writes a simple message with the results of a form. The user and the vote are mailed to john.doe.

mail.jsp
<%@ taglib prefix="ct" uri="/WEB-INF/tags.tld" %>
<h1>Testing Mail</h1>

<ct:mail to="john.doe@localhost" subject="survey response">
<item>
  <user><%= request.getParameter("user") %></user>
  <vote><%= request.getParameter("vote") %></vote>
</item>
</ct:mail>

Subject: survey
   Date: Mon, 11 Sep 2000 18:06:52 -0700
   From: Resin Account <resin@localhost>
     To: john.doe@localhost

<item>
  <user>Fred Smith</user>
  <vote>Gryffindor</vote>
</item>

Implementation of the Mail Tag

The MailTag has two required attributes, subject and to. As usual, they are set using the bean design patterns setSubject() and setTo().

Mailing uses Resin's VFS. The mailto: scheme sends mail to any user account.

test/MailTag.java
package test;

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

import com.caucho.vfs.*;

public class MailTag extends BodyTagSupport {
  private String to;
  private String subject;

  public void setTo(String to)
  {
    this.to = to;
  }

  public void setSubject(String subject)
  {
    this.subject = subject;
  }
  
  public int doAfterBody()
    throws JspException
  {
    String body = getBodyContent().getString();

    Path mailto = Vfs.lookup("mailto:" + to);
    try {
      WriteStream os = mailto.openWrite();
      os.setAttribute("subject", subject);

      os.print(body);

      os.close();
    } catch (IOException e) {
      throw new JspException(e.toString());
    }

    return SKIP_BODY;
  }
}

Attribute Configuration

Each expected attribute needs an entry in the .tld. In this case, both to and subject are required.

WEB-INF/tag.tld
<taglib>
  <tag>
    <name>mail</name>
    <tagclass>test.MailTag</tagclass>
    <attribute>
      <name>to</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>subject</name>
      <required>true</required>
    </attribute>
  </tag>
</taglib>

Summary

  • BodyTags can capture arbitrary JSP output.
  • After the contents complete, JSP calls doAfterBody.
  • getBodyContent() contains the JSP body.
  • VFS mailto: sends mail.

Attr Tag
Hello Tag
XSL Tag
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.