www.pudn.com > JMail4Email.rar > MailControllerServlet.java
package ezmail;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import ezmail.Constants;
/**
* This servlet is the web front component for the ezmail web app. It expects the following parameter(s):
*
*
* - action - the name of the action to perform
*
*/
public class MailControllerServlet extends HttpServlet {
private HashMap actionTable;
private HashMap pageTable;
/**
* The method performs the following operations:
*
*
* - Create the WebAction implementations and place in HashMap
*/
public void init() throws ServletException {
try {
log("in the init() method");
// Create the Action implementations and place in HashMap
actionTable = new HashMap();
actionTable.put(Constants.LOGIN_KEY, new LoginAction());
actionTable.put(Constants.SEND_MESSAGE_KEY, new SendMessageAction());
actionTable.put(Constants.VIEW_MESSAGE_LIST_KEY, new ViewMessageListAction());
actionTable.put(Constants.VIEW_MESSAGE_DETAIL_KEY, new ViewMessageDetailAction());
actionTable.put(Constants.LOGOUT_KEY, new LogoutAction());
// Setup the destination pages
pageTable = new HashMap();
pageTable.put(Constants.LOGIN_KEY, Constants.HOME_PAGE);
pageTable.put(Constants.SEND_MESSAGE_KEY, Constants.SEND_MESSAGE_PAGE);
pageTable.put(Constants.VIEW_MESSAGE_LIST_KEY, Constants.VIEW_MESSAGE_LIST_PAGE);
pageTable.put(Constants.VIEW_MESSAGE_DETAIL_KEY, Constants.VIEW_MESSAGE_DETAIL_PAGE);
pageTable.put(Constants.LOGOUT_KEY, Constants.LOGOUT_PAGE);
}
catch (Exception exc) {
exc.printStackTrace();
throw new ServletException(exc.getMessage());
}
}
protected void process(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// retrieve the desired Action implementation
String actionParam = request.getParameter(Constants.ACTION_PARAM);
Action tempAction = (Action) actionTable.get(actionParam);
// now let's perform the action!
// note: getServletContext() is inherited from javax.servlet.GenericServlet
tempAction.perform(getServletContext(), request, response);
// Dispatch/forward the request to destination page
String destPage = (String) pageTable.get(actionParam);
System.out.println("destPage = " + destPage);
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher(destPage);
dispatcher.forward(request, response);
}
catch (ActionException exc) {
exc.printStackTrace();
throw new ServletException(exc.getMessage());
}
}
/**
* Forwards requests to the process(...) method
*/
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request, response);
}
/**
* Forwards requests to the process(...) method
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request, response);
}
}