HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
PROFESSIONAL WAP PART 6 - A WAP-BASED E-MAIL APPLICATION

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Web mail accounts such as the ubiquitous 'Hotmail', or 'Yahoo! Mail' are fast becoming the most popular kind of e-mail - there are almost 170 million subscribers to this type of account.


This free tutorial is a sample from the book Professional WAP.


Web-based mail allows users convenient web access to their mail account without any messy machine configuration issues. Our second sample application implements a simple WAP mail system, allowing access to an SMTP/POP3-based e-mail account.

This example uses Java servlets and JavaMail to:

  • Compose, send and reply to mail via an SMTP server
  • View an inbox and read mail using a POP3 service
  • Determine the number of messages waiting in the inbox
  • Delete mail

Diagram of WAPMail's Functionality

Below is a diagram illustrating the functionality of the basic web mail system we will create in WAPMail.java. The user can logon to the system, view their POP3 inbox, read mail, reply to, compose, send, and delete e-mail.

WAPMail.java

WAPMail provides all the basic functionality described above, but the code has been designed for illustration rather than deployment in a production environment. It should be noted that the code is limited in the following ways:

  • It has minimal error checking.
  • There is no guarantee that a WML deck of more that 1400 bytes will be accepted by the WAP browser. Large emails or a long list of emails in the inbox will cause problems.
  • It is not implemented for maximum speed and efficiency (for example, use of String concatenation rather than StringBuffers)
  • It can only be used by a single user - session information is held in a static class variable. We would need to use some session tracking code to make this code multi-user and scaleable.
  • It has some problems if the browser does not correctly respond to Cache-Control response headers.

WAPMail.java is quite a long piece of code. Below, we'll step through it one piece at a time so we might clearly understand what the code is doing.

The first piece of code shows the servlet's main decision code. When the servlet receives a HTTP POST or GET request, it executes the doPost() or doGet() respectively. If the user has not logged in, and had the necessary session information created, they are shown the splash screen, which requires them to login to proceed:

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.wrox.util.WML;

public class WAPMail extends HttpServlet implements SingleThreadModel
{
private static String inboxString = "INBOX";
private static UserSessionData _userSessionData; 
public void doGet (HttpServletRequest request,
    HttpServletResponse response)
  throws ServletException, IOException
  {
this.doPost(request,response);
  }

public void doPost (HttpServletRequest request,
    HttpServletResponse response)
  throws ServletException, IOException
  {
UserSessionData userSessionData = getUserSessionData();
String action = request.getParameter("action");

//Main decision making "loop"
try {
  //SHOW SPLASH SCREEN

  if ((action == null) ||
   ((!action.equalsIgnoreCase("login")) && 
   (userSessionData == null))) {
   this.splashScreen(request, response);
  }
  //LOGIN
  else if (action.equalsIgnoreCase("login")) {
   this.login(request, response);
  }
  //LOGOUT
  else if (action.equalsIgnoreCase("logout")) {
   this.logout(request, response);
  }
  //SHOW MAIN MENU
  else if (action.equalsIgnoreCase("mainmenu")) {
   this.mainMenu(request, response, 
    this.getUserSessionData());
  }
  //COMPOSE
  else if (action.equalsIgnoreCase("compose")) {
   this.compose(request, response);
  }
  //SEND
  else if (action.equalsIgnoreCase("send")) {
   this.send(request, response, 
    this.getUserSessionData());
  }
  //VIEW IN BOX
  else if (action.equalsIgnoreCase("viewinbox")) {
   this.viewInbox(request, response, 
    this.getUserSessionData());
  }
  //READ
  else if (action.equalsIgnoreCase("read")) {
   this.read(request, response, this.getUserSessionData());
  }
  //DELETE
  else if (action.equalsIgnoreCase("delete")) {
   this.deleteMessage(request, response, 
    this.getUserSessionData());
  }
  //REPLY
  else if (action.equalsIgnoreCase("reply")) {
   this.replyToMessage(request, response,
    this.getUserSessionData());
  }
} catch (MessagingException me) {
  me.printStackTrace();
}
  }

  public void splashScreen(HttpServletRequest request,
    HttpServletResponse response)
throws ServletException, IOException, MessagingException
  {
  this.splashScreen(request, response, "");
  }
}

public void splashScreen(HttpServletRequest request,
  HttpServletResponse response, String message)<
throws ServletException, IOException, MessagingException {

  WML wml = new WML();
  wml.addCard("WAPMailSplash");
  wml.println("<do type=\"accept\" label=\"Login\">" +

"<go href=\"" + request.getRequestURI() + "\" method=\"post\">" +
  "<postfield name=\"action\" value=\"login\"/>" +
  "<postfield name=\"uid\" value=\"$uid\"/>" +
  "<postfield name=\"pwd\" value=\"$pwd\"/>" +
  "</go>" +
  "</do>" +
  "<p align=\"center\">" +
  "WAP E-mail" +
  "</p>");
  if (message != "") {
wml.println("<p align=\"left\">" +
   "<i>" + message + "" +
   "</p>");
  }
wml.println("<p>" +
  "Username:" +
  "<input name=\"uid\" title=\"user name\"/><br/>" +
  "Password:" +
  "<input name=\"pwd\" type=\"password\" title=\"password\"/><br/>" +
  "</p>");
  wml.endCard();
  wml.outputWML(response, true);
}

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
MICROSOFT INTERNET EXPLORER 5.0 INTERNET INTRODUCTION
This course is a self-paced introduction to browsing the internet and sending e-mail using MS Internet Explorer a....
I-NET+ MODULE 7 - THE WORLD WIDE WEB SERVICE
On completion of this module, readers will be able to: understand how the World Wide Web service works, understan....
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Sunday 7th September 2008  © COPYRIGHT 2008 - VISUALSOFT