IntroductionThe JavaMailTM 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 SpecificationThe EWA supports the basic functionality of an email application, such as login and checking and sending email. More specifically, the following functionality is supported:
Application ArchitectureThe 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 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 ModelMailUserBeanThis is the java bean that stores mail user information, such as hostname, username, password, session and protocol. It has methods to get and set these parameters. For the purposes of this demonstration, the protocol has been hardcoded to be IMAP. This bean implements the user login and logout methods. It uses mailSession and Store objects to validate the hostname, username and password combination.
Tag LibraryThe tag library contains the following custom tags:
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:
Each of the tags has its own handler classes which implement the actions of the tags. For example, the 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.
AttachmentServletThe 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.
The ViewThe 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 ControllerThe 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 Application DeploymentThis application was developed using the following:
To run this application, you need the following:
Here are a few snapshots of a sample run. The entrypoint is the index.html. A sucessful login leads to the inbox. If login fails, the error page shows up. The user then has an option of viewing error details. From within the inbox, the user has the option of either composing a message or logging out. 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 WEB-INF directory contains:
The EWA (webapp.zip) is available for download. ConclusionThis 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. Have a question about programming? Use Java Online Support. |
聯(lián)系客服