home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-10-17 | 6.0 KB | 234 lines |
- package symantec.sourcebook.servlet;
-
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class ESDServlet extends HttpServlet
- {
-
- // constants for pages and their numbers
-
- protected static int NOFORM_PAGE_NUM = 0;
- protected static int ENTERINFO_PAGE_NUM = 1;
- protected static int VERIFYINFO_PAGE_NUM = 2;
- protected static int OWNER_PAGE_NUM = 3;
- protected static int DOWNLOAD_PAGE_NUM = 4;
- protected static int SUCCESS_PAGE_NUM = 5;
- protected static int ERROR_PAGE_NUM = 6;
-
- /**
- * Class names of the classes that handle each page.
- *
- */
- public String[] pageHandlerNames = {
- "symantec.sourcebook.servlet.PHNoForm",
- "symantec.sourcebook.servlet.PHEnterInfo",
- "symantec.sourcebook.servlet.PHVerifyInfo",
- "symantec.sourcebook.servlet.PHOwner",
- "symantec.sourcebook.servlet.PHDownload",
- "symantec.sourcebook.servlet.PHSuccess",
- "symantec.sourcebook.servlet.PHError"};
-
-
-
-
-
- /**
- * Returns a brief description of this servlet.
- * Typically used by servlet administrative functions.
- *
- */
-
- public String getServletInfo()
- {
- return "Sample ESD servlet";
- }
-
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {
- // handle the GET the same way we handle POSTS
-
- doPost(req,resp);
-
- }
-
- /**
- * Handle client POST request.
- * This is the main function of this servlet.
- * It determines the page being POSTed,
- * obtains the PageHandler for the page. Calls
- * the PageHandler to validate the page. Calls
- * the PageHandler to obtain the next page to be served
- * and serves it.
- *
- *
- */
-
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {
-
- // don't accept giant posts, they're either errors or bogus
-
- if(req.getContentLength() > 4096)
- {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
- return;
- }
-
- // determine which form has been submitted
-
- int pageNumber = getPageNumber(req);
-
- // get handler for that page
-
- PageHandler pageHandler = getPageHandler(pageNumber);
-
- // validate the contents of the form
-
- Object pageData = pageHandler.validate(getServletConfig(),req,resp);
-
- // Ask the page handler what page should be served
- // next (often determined in validate)
-
- pageNumber = pageHandler.nextPage();
-
- // get the page handler for the next page to be served...
-
- pageHandler = getPageHandler(pageNumber);
-
- // ...and serve it up
-
- pageHandler.serve(pageData,resp);
-
-
-
- }
-
- /**
- * This method obtains the page handler for the specified page number.
- *
- */
-
- protected PageHandler getPageHandler(int pageNumber) throws ServletException
- {
-
-
- // get the name of the class that handles this page number
- // this will be a subclass of PageHandler
-
- String pageHandlerName = pageHandlerNames[pageNumber];
-
-
- try
- {
- // instantiate the handler for this page
-
- PageHandler pageHandler = (PageHandler)(Class.forName(pageHandlerName)).newInstance();
-
- return pageHandler;
- }
- catch(Exception e)
- {
-
- }
-
-
- // some sort of exception occured trying to instantiate
- throw new ServletException("(Order) Internal servlet error (1)");
-
- }
-
-
-
- // perform one-time servlet initialization
-
- public void init(ServletConfig config) throws ServletException
- {
- // perform standard init
- super.init(config);
-
- // try to initialize the recent transaction info
-
- try
- {
- Transaction.InitializeTransactions();
- }
- catch(Exception e)
- {
- // Some exception occured. Let server know that this servlet is unavailable.
-
- UnavailableException u = new UnavailableException(this,e + " " + e.getMessage());
- throw u;
- }
- }
-
- // perform one-time servlet shutdonw
-
- public void destroy()
- {
- try
- {
- Transaction.WriteTransactions();
- }
- catch(IOException e)
- {
- }
-
- super.destroy();
- }
-
-
- /**
- * Return the current page number. This number is embeded in a hidden
- * field on each of the pages served.
- */
-
- public int getPageNumber(HttpServletRequest request)
- {
-
- int pageNumber = NOFORM_PAGE_NUM;
-
- // obtain the value of the "page" field on the current form
-
- String pageString = request.getParameter("page");
-
- // if no page parameter is found, this is either
- // the initial request (so we should retrun page zero)
- // or it's a re-download request (so we should return
- // the re-download page.)
-
- if(pageString == null)
- {
- // we've set up the HTML so that the download page
- // has a specific URI... this allows the user to bookmark
- // the page and return there if the initial download attempt fails
-
- if(request.getRequestURI().startsWith("/servlet/purchase/down")) pageNumber = SUCCESS_PAGE_NUM;
- return pageNumber;
- }
-
- // ok, we have a page number, make sure its good and within
- // range. (It always should be unless we made an error on one
- // of our HTML forms, or a user has modified the form)
-
- try
- {
- pageNumber = Integer.parseInt(pageString);
- if(pageNumber >= pageHandlerNames.length) pageNumber = NOFORM_PAGE_NUM;
- }
- catch(NumberFormatException e)
- {
- pageNumber = NOFORM_PAGE_NUM;
- }
-
-
- return pageNumber;
-
- }
-
-
-
- }