JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»
12.5.3 J2EE send tag

Although J2EE greatly reduces the amount of code required in order to send an email, we still want to wrap this up in some neat tags that will make this attainable from our JSP pages. We will do this by extending the <send> tag from chapter 7, in a manner described in listing 12.14.

Listing 12.14 Implementation of the J2EE Send tag

package book.j2ee;

import book.util.LocalStrings;

import java.util.Properties;

import javax.mail.Transport;

import javax.mail.Session;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage; import javax.mail.internet.InternetAddress; import javax.mail.internet.AddressException;

import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException;

import book.mail.BodyJavaMailTag; import javax.naming.*;

public class J2EEMailTag extends BodyJavaMailTag { protected String sessionName; static LocalStrings ls =


public void setSessionName(String sessionName){ this.sessionName=sessionName;


protected void checkParameters() throws JspException {

try {

if(null == sessionName){

session=Session.getDefaultInstance(new Properties(),null); О }else{

InitialContext context=new InitialContext(); session=(Session)context.lookup(



recipient = new InternetAddress(to); if(from!=null){

sender=new InternetAddress(from); О


} catch(NamingException ne) { throw new JspTagException(


} catch(AddressException ae){ throw new JspTagException(


} catch(MessagingException me){ throw new JspTagException(




protected void sendMail(Session session,

InternetAddress sender,

InternetAddress recipient,

String content,

String subject) throws MessagingException


Custom tags and J2EE

MimeMessage message = new MimeMessage(session); if(null!=sender){



message.addRecipient(Message.RecipientType.TO, recipient); if(null != subject) {


} else {



if(null == content) { // Empty body content = «»;

Скачать в pdf «JSP Tag Libraries»