You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Edson Alves Pereira <ea...@panamericano.com.br> on 2003/05/28 19:20:45 UTC

Taglib using BodyTagSupport

	Hello folks, i´m using tomcat-4.1.18 and i tried to create a tag
that would read its body and do some work. But bodyContent always cames
null, i´m following this example:

package com.acme.tag;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.naming.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 * A Tag for sending Mail messages
 * @author Magnus Rydin
 */
public class SendMailTag extends BodyTagSupport {
    private String mimeType = "text/plain";
    private Session session = null;
    private Message message = null;
    private String to = null;
    private String cc = null;
    private String bcc = null;
    private String from = null;
    private String subject = null;
    private String sessionLocation = null;
    private Multipart multipart = new MimeMultipart();
    private InitialContext context = null;

    /** Constructor */
    public SendMailTag() {
        super();
    }

    /**
     * Method used by the JSP container to set the to variable
     * @param to the TO address
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * Method used by the JSP container to set the cc variable
     * @param cc the CC address
     */
    public void setCc(String cc) {
        this.cc = cc;
    }

    /**
     * Method used by the JSP container to set the bcc variable
     * @param bcc the BCC address
     */
    public void setBcc(String bcc) {
        this.bcc = bcc;
    }

    /**
     * Method used by the JSP container to set the from variable
     * @param from the FROM address
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * Method used by the JSP container to set the subject variable
     * @param from the FROM address
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * Method used by the JSP container to set the mimeType variable
     * @param mimeType the mimetype to use
     */
    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    /**
     * Method used by the JSP container to set the session variable
     * @param mimeType the mimetype to use
     */
    public void setSession(String session) {
        this.sessionLocation = session;
    }

    /** Method used by subTags to add parts to the message */
    public Multipart getMultipart() {
        return multipart;
    }

    /**
     * Method called at start of tag
     * @return EVAL_BODY_TAG
     * @throws JspException if mail session could not be found
     */
    public int doStartTag() throws JspTagException {
        try {
            context = new InitialContext();
            //get the mail session
            if (sessionLocation != null) {
                session = (Session)context.lookup("java:comp/env/mail/" +
sessionLocation);
            } else {
                session = Session.getDefaultInstance(
                    new Properties(), null);
            }
        } catch (NamingException ne) {
            throw new JspTagException(ne.getMessage());
        }
        message = new MimeMessage(session);
        return EVAL_BODY_TAG;
    }

    /**
     * Method called at end of body
     * @return SKIP_BODY
     * @throws JspException if message content could not be set
     */
    public int doAfterBody() throws JspTagException {
        try {
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setContent(bodyContent.getString(), mimeType);
            multipart.addBodyPart(mbp, 0);
            message.setContent(multipart);
        } catch (MessagingException me) {
            throw new JspTagException(me.getMessage());
        }
        // clear body
        bodyContent.clearBody();
        return SKIP_BODY;
    }

    /**
     * Method called at end of tag
     * @return EVAL_PAGE
     * @throws JspException if there is an illegal address or if the mail
could not be sent
     */
    public int doEndTag() throws JspTagException {
        try {
            if (from != null) {
                message.setFrom(
                    new InternetAddress(from));
            }
            if (subject != null) {
                message.setSubject(subject);
            }
            addRecipients(to,Message.RecipientType.TO);
            addRecipients(cc,Message.RecipientType.CC);
            addRecipients(bcc,Message.RecipientType.BCC);
            Transport.send(message);
        } catch (AddressException ae) {
            throw new JspTagException(ae.getMessage());
        } catch (MessagingException me) {
            throw new JspTagException(me.getMessage());
        }
        return EVAL_PAGE;
    }

   /**
     * Adds one or more recipients to the message.
     * Multiple recipients should be separated by comma-signs
     * @throws AddressException if there is an illegal address
     * @throws MessagingException if the address cannot be added to the
Message
     */
    public void addRecipients(String addresses, Message.RecipientType type)
throws AddressException, MessagingException
    {
      if (addresses != null) {
        StringTokenizer st = new java.util.StringTokenizer(addresses, ",");
        while (st.hasMoreTokens()) {
          InternetAddress inetAddress=new InternetAddress(st.nextToken());
          message.addRecipient(type,inetAddress);
        }
      }
    }
}


	Any idea?

	Best regards,
	Edson

Re: Taglib using BodyTagSupport

Posted by Bill Barker <wb...@wilshire.com>.
The class looks Ok to me.  How are you referring to it in your JSP page?  It
should fail if you are doing something like:
  <my:sendMail to="eapereia@panamericono.com.br"
                        from="eapereia@panamericono.com.br"
                        subject="test" />

(e.g. an empty body).

"Edson Alves Pereira" <ea...@panamericano.com.br> wrote in message
news:4EFE7B26CE175F4B9653BD51D199CCFC0154DDC7@panms2505a.panamericano.com.br
...
Hello folks, i�m using tomcat-4.1.18 and i tried to create a tag
that would read its body and do some work. But bodyContent always cames
null, i�m following this example:

package com.acme.tag;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.naming.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 * A Tag for sending Mail messages
 * @author Magnus Rydin
 */
public class SendMailTag extends BodyTagSupport {
    private String mimeType = "text/plain";
    private Session session = null;
    private Message message = null;
    private String to = null;
    private String cc = null;
    private String bcc = null;
    private String from = null;
    private String subject = null;
    private String sessionLocation = null;
    private Multipart multipart = new MimeMultipart();
    private InitialContext context = null;

    /** Constructor */
    public SendMailTag() {
        super();
    }

    /**
     * Method used by the JSP container to set the to variable
     * @param to the TO address
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * Method used by the JSP container to set the cc variable
     * @param cc the CC address
     */
    public void setCc(String cc) {
        this.cc = cc;
    }

    /**
     * Method used by the JSP container to set the bcc variable
     * @param bcc the BCC address
     */
    public void setBcc(String bcc) {
        this.bcc = bcc;
    }

    /**
     * Method used by the JSP container to set the from variable
     * @param from the FROM address
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * Method used by the JSP container to set the subject variable
     * @param from the FROM address
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * Method used by the JSP container to set the mimeType variable
     * @param mimeType the mimetype to use
     */
    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    /**
     * Method used by the JSP container to set the session variable
     * @param mimeType the mimetype to use
     */
    public void setSession(String session) {
        this.sessionLocation = session;
    }

    /** Method used by subTags to add parts to the message */
    public Multipart getMultipart() {
        return multipart;
    }

    /**
     * Method called at start of tag
     * @return EVAL_BODY_TAG
     * @throws JspException if mail session could not be found
     */
    public int doStartTag() throws JspTagException {
        try {
            context = new InitialContext();
            //get the mail session
            if (sessionLocation != null) {
                session = (Session)context.lookup("java:comp/env/mail/" +
sessionLocation);
            } else {
                session = Session.getDefaultInstance(
                    new Properties(), null);
            }
        } catch (NamingException ne) {
            throw new JspTagException(ne.getMessage());
        }
        message = new MimeMessage(session);
        return EVAL_BODY_TAG;
    }

    /**
     * Method called at end of body
     * @return SKIP_BODY
     * @throws JspException if message content could not be set
     */
    public int doAfterBody() throws JspTagException {
        try {
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setContent(bodyContent.getString(), mimeType);
            multipart.addBodyPart(mbp, 0);
            message.setContent(multipart);
        } catch (MessagingException me) {
            throw new JspTagException(me.getMessage());
        }
        // clear body
        bodyContent.clearBody();
        return SKIP_BODY;
    }

    /**
     * Method called at end of tag
     * @return EVAL_PAGE
     * @throws JspException if there is an illegal address or if the mail
could not be sent
     */
    public int doEndTag() throws JspTagException {
        try {
            if (from != null) {
                message.setFrom(
                    new InternetAddress(from));
            }
            if (subject != null) {
                message.setSubject(subject);
            }
            addRecipients(to,Message.RecipientType.TO);
            addRecipients(cc,Message.RecipientType.CC);
            addRecipients(bcc,Message.RecipientType.BCC);
            Transport.send(message);
        } catch (AddressException ae) {
            throw new JspTagException(ae.getMessage());
        } catch (MessagingException me) {
            throw new JspTagException(me.getMessage());
        }
        return EVAL_PAGE;
    }

   /**
     * Adds one or more recipients to the message.
     * Multiple recipients should be separated by comma-signs
     * @throws AddressException if there is an illegal address
     * @throws MessagingException if the address cannot be added to the
Message
     */
    public void addRecipients(String addresses, Message.RecipientType type)
throws AddressException, MessagingException
    {
      if (addresses != null) {
        StringTokenizer st = new java.util.StringTokenizer(addresses, ",");
        while (st.hasMoreTokens()) {
          InternetAddress inetAddress=new InternetAddress(st.nextToken());
          message.addRecipient(type,inetAddress);
        }
      }
    }
}


Any idea?

Best regards,
Edson





---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org