Email Web Application Using JSP Tag Libraries

   
   

Articles Index


 

Introduction



The JavaMail TM API is a Java Standard Extension. It provides a strictly protocol-independent method of sending and receiving email. JavaMail's layered architecture allows the use of various message access protocols, like POP3 and IMAP, and message transfer protocols like SMTP. JavaMail interacts with message content through the JavaBeans Activation Framework (JAF). JAF provides a uniform way of determining message type and encapsulating it. For more information, see the JavaBeans Activation Framework.

JavaServer Pages (JSP ) allow web developers to develop content-rich, dynamic pages rapidly and easily. JSP uses XML-like tags to encapsulate the logic that generates web content. JSP pages separate the page logic from its design and display, which prevents the overlapping of roles between web designers and programmers. Designers design the web pages and programmers add the logic and code to them. For more information and useful tutorials on JavaServer Pages technology, see JavaServer Pages Dynamically Generated Web Content.

This paper explores the possibilities of combining these two technologies and creating a web deployable email application that uses the JavaMail API and custom JSP tag libraries for presentation. JSP pages are intended to provide a "declarative, presentation-centric method of developing servlets." An ideal JSP does not contain any inline code or scriptlets. Instead, the functionality and business logic is executed in tags which are either predefined in the API or in custom tag libraries. The Email Web Application (EWA) presented in this paper uses a custom tag library. The tags are implemented using the JavaMail API.

 

Functional Specification



The EWA supports the basic functionality of an email application, such as login and checking and sending email. More specifically, the following functionality is supported:

  • Login to an IMAP server
  • List all the messages in the INBOX folder
  • View selected messages
  • Retrieve and view attachments from selected messages
  • Compose and send messages


 

Application Architecture



The EWA is a three-tiered web application. It resides on a web application server that interacts with an IMAP mail server. The clients are web pages generated by JSP pages deployed in a JSP/Servlet engine.


The EWA utilizes a derivative of the Model-View-Controller (MVC) design pattern. In the MVC paradigm, the user input, the modeling of the external world, and the visual feedback to the user are explicitly separated and handled by three types of objects, each specialized for its task.

The view manages the graphical and/or textual output to the portion of the bitmapped display that is allocated to its application. The controller interprets the mouse and keyboard inputs from the user, commanding the model and/or the view to change as appropriate. Finally, the model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

In this application, the model consists of the IMAP message store and the MailUserBean, the tag library and the AttachmentServlet, which access/manage the message store. These are the components that are used either to store or retrieve data. The view consists of the HTML and JSP components that provide the user interface. The controller is the FilterServlet, which validates the users' login status.

 

The Model

MailUserBean

SessionStore

 URLName url = new URLName(protocol,
         getHostname(), -1, mbox,
         getUsername(),getPassword());
 Properties props = System.getProperties();
 props.put("mail.smtp.host",getHostname());
 Session session = 
 Session.getDefaultInstance(props,null); 
 Store store = session.getStore(url);
 store.connect();




 

Tag Library



 

  • message: used to read a message, tag handler classes: MessageTag.java, MessageTEI.java
  • listmessages: used to iterate through list of messages, handler classes: ListMessagesTag.java, ListMessagesTEI.java
  • sendmail: used to send messages, handler class: SendTag.java


The tag descriptor file defines the name of the tag, its handler class(es), attributes, body content, and so on. For example, the listmessages tag is defined thus:

<tag>
  <name>listmessages</name>
  <tagclass>ListMessagesTag</tagclass>
  <teiclass>ListMessagesTEI</teiclass>
  <bodycontent>JSP</bodycontent>
  <info>
           A listmessages tag
  </info>
  <attribute>
  <name>id</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
..... 
.....
</tag>




Each of the tags has its own handler classes which implement the actions of the tags. For example, the listmessages tag is implemented in ListMessagesTag.java.

The TEI classes are the Tag Extra Info classes that provide information about the scripting variables that are created/modified at runtime. It is required for tags that define scripting variables. This information is used during the translation phase of JSP.

VariableInfo info = 
new VariableInfo(data.getId(),"MessageInfo", 
         true,VariableInfo.NESTED);
VariableInfo[] varInfo = { info };
return varInfo;




 

AttachmentServlet

The AttachmentServlet gets the stream from a given part of a multipart message and pushes it out to the browser with the correct content type. This servlet is used to display attachments and it relies on the browser's content handling capabilities.

Message msg = 
      mailuser.getFolder().getMessage(msgNum);

Multipart multipart = (Multipart)msg.getContent();
Part part = multipart.getBodyPart(partNum);

String sct = part.getContentType();
if (sct == null)   {
     out.println("invalid part");
     return;
}
ContentType ct = new ContentType(sct);
 
response.setContentType(ct.getBaseType());
InputStream is = part.getInputStream();
int i;
while ((i = is.read()) != -1)
     out.write(i);
out.flush();
out.close();




 

The View



The view consists of JavaMail.html, which is the initial point of entry. This page requires the user to enter a username, password and IMAP hostname. After the login information is validated, the user navigates through a series of JSP pages.

 

The Controller



The controller is the FilterServlet. This servlet is used to determine whether the user is logged in before forwarding the request to the selected URL. The doPost method handles the POST submission from two forms: login.jsp and compose.jsp. The doGet method handles the GET requests from the client.

 

Application Deployment



This application was developed using the following:

 

  • JavaMail 1.2
  • Servlet 2.2
  • JavaServer Pages 1.1
  • JavaBeans Activation Framework (JAF) 1.0.1


To run this application, you need the following:

 

  1. A JSP/Servlet implementation can be run as a standalone or as an extension to a webserver. For this application, Tomcat 3.2.1 was used.
  2. An IMAP mail server.


Here are a few snapshots of a sample run. The entrypoint is the index.html.

The EWA is comprised of an HTML document and several web components (servlets and JSP and custom tags). Apart from these, it has two directories, META-INF and WEB-INF.

The META-INF directory contains:

  • The manifest file for the application.


The WEB-INF directory contains:

  • The web.xml file which contains the configuration and deployment information for the application.
  • The classes directory which contains servlet and utility classes used by the web application.
  • The lib directory which contains the java archive file (jtl.jar) for the custom tag library.


 

Conclusion



This application demonstrates a way to develop an application using the JavaMail API and JSP. An effort was made to implement most of the actions in the tag library and thus minimize inline code. However, attachment handling was done in a servlet instead of a tag. This was more a matter of convenience than design. The functionality of AttachmentServlet could alternatively be implemented in a tag.

The next release of the JavaMail API will be bundled with a demo application similar to the one discussed in this paper. It will contain more features and certain modifications to this version. Please see JavaMail API and Internet Mail Resources for updates.