home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
CreditCheck.java
< prev
next >
Wrap
Text File
|
1998-10-17
|
4KB
|
178 lines
package symantec.sourcebook.creditcheck;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreditCheck extends HttpServlet
{
protected static int FORMINPUT_PAGE_NUM = 0;
protected static int CARDENTRY_PAGE_NUM = 1;
protected static int ERROR_PAGE_NUM = 2;
protected static int SUCCESS_PAGE_NUM = 3;
/**
* Class names of the classes that handle each page.
*
*/
public String[] pageHandlerNames = {
"symantec.sourcebook.creditcheck.PHFormInput",
"symantec.sourcebook.creditcheck.PHCardEntry",
"symantec.sourcebook.creditcheck.PHError",
"symantec.sourcebook.creditcheck.PHSuccess"
};
public String getServletInfo()
{
return "Dummy credit check";
}
/**
* Respond to client GET requests.
* All get requests are handled the same way as
* client POST requets.
*
*/
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)");
}
/**
* Return the current page number. This number is embeded in a hidden
* field on each of the pages served.
*/
protected int getPageNumber(HttpServletRequest request)
{
int pageNumber = 0;
// obtain the value of the "page" field on the current form
String pageString = request.getParameter("page");
// if no page parameter is found, go with page 0
if(pageString == null)
{
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 = 0;
}
catch(NumberFormatException e)
{
pageNumber = 0;
}
return pageNumber;
}
}