home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / CustomerInfo.java < prev    next >
Text File  |  1997-10-25  |  4KB  |  139 lines

  1. /**
  2.  * CustomerInfo: A Java class that demonstrates accessing form variables, cookies, etc. with
  3.  * the Java ASP Component Framework.
  4.  */
  5.  
  6. package IISSample;
  7.  
  8. import aspcomp.*;
  9.  
  10.  
  11. public class CustomerInfo
  12. {
  13.     private static final String INTERNAL_ERROR_MSG = 
  14.         new String("Internal error condition detected.");
  15.  
  16.     // See if the user has already registered with this site, by checking 
  17.     // for the presence of our cookie.
  18.     public boolean checkIfRegistered()
  19.     {
  20.         Request request = AspContext.getRequest();
  21.         Response response = AspContext.getResponse();
  22.         CookieDictionary cookDict = request.getCookies();
  23.         Cookie c = null;
  24.  
  25.         // Look for our cookie
  26.         try 
  27.         {
  28.             c = cookDict.getCookie("CustInfo");
  29.         }
  30.  
  31.         catch (ClassCastException cce) 
  32.         {
  33.             response.write(INTERNAL_ERROR_MSG);
  34.             throw new RuntimeException();
  35.         }
  36.  
  37.         String strCookieValue = c.getValue();
  38.  
  39.         if (!strCookieValue.equals("")) 
  40.         {
  41.             // The customer has registered before.
  42.             return true;
  43.         }
  44.         else {
  45.             // The customer has NOT registered before.
  46.             return false;
  47.         }
  48.     }
  49.  
  50.     // Called in response to form submission, to process the
  51.     // data and place it into the cookie.
  52.     public boolean processForm()
  53.     {
  54.         Request request = AspContext.getRequest();
  55.         Response response = AspContext.getResponse();
  56.  
  57.         if (request.getTotalBytes() == 0) {
  58.             // No data here -- make the customer enter it
  59.             return false;
  60.         }
  61.  
  62.         CookieDictionary cookDict = request.getCookies();
  63.         RequestDictionary form = request.getForm();
  64.         Cookie cookCust = null;
  65.  
  66.         try 
  67.         {
  68.             cookCust = cookDict.getCookie("CustInfo");
  69.         }
  70.         catch (ClassCastException cce) 
  71.         {
  72.             response.write(INTERNAL_ERROR_MSG);
  73.             throw new RuntimeException();
  74.         }
  75.  
  76.         // Note: could return false if mandatory fields 
  77.         // aren't present
  78.         cookCust.setItem("Prefix", form.getString("Prefix"));
  79.         cookCust.setItem("FName",  form.getString("FName"));
  80.         cookCust.setItem("MName",  form.getString("MName"));
  81.         cookCust.setItem("LName",  form.getString("LName"));
  82.         cookCust.setItem("Suffix", form.getString("Suffix"));
  83.         cookCust.setItem("Addr1",  form.getString("Addr1"));
  84.         cookCust.setItem("Addr2",  form.getString("Addr2"));
  85.         cookCust.setItem("AptNo",  form.getString("AptNo"));
  86.         cookCust.setItem("City",   form.getString("City"));
  87.         cookCust.setItem("State",  form.getString("State"));
  88.         cookCust.setItem("ZIP",    form.getString("ZIP"));
  89.         cookCust.setItem("Birth",  form.getString("Birth"));
  90.         cookCust.setItem("SocSec",  form.getString("SocSec"));
  91.  
  92.         // Make sure the cookie is sent by the browser to all URLs
  93.         // on this site; avoids any case-sensitivity problems
  94.         cookCust.setPath("/");
  95.  
  96.         return true;
  97.     }
  98.  
  99.     // Use the information stored in the cookie to retrieve the 
  100.     // customer's full name.
  101.     public String getFullName()
  102.     {
  103.         Request request = AspContext.getRequest();
  104.         Response response = AspContext.getResponse();
  105.         CookieDictionary cookDict = request.getCookies();
  106.         RequestDictionary form = request.getForm();
  107.         Cookie cookCust = null;
  108.  
  109.         try 
  110.         {
  111.             cookCust = cookDict.getCookie("CustInfo");
  112.         }
  113.  
  114.         catch (ClassCastException cce) 
  115.         {
  116.             response.write(INTERNAL_ERROR_MSG);
  117.             throw new RuntimeException();
  118.         }
  119.  
  120.         // Note: Framework returns an empty string if the item isn't 
  121.         // found in the cookie
  122.  
  123.         String Prefix = cookCust.getItem("Prefix");
  124.         String FName = cookCust.getItem("FName");
  125.         String MName = cookCust.getItem("MName");
  126.         String LName = cookCust.getItem("LName");
  127.         String Suffix = cookCust.getItem("Suffix");
  128.  
  129.         String fullName = Prefix + (Prefix.equals("") ? "" : " ") + 
  130.                    FName + (FName.equals("") ? "" : " ") +
  131.                    MName + (MName.equals("") ? "" : " ") +
  132.                    LName + (LName.equals("") ? "" : " ") +
  133.                    Suffix;
  134.  
  135.         return(fullName);
  136.     }
  137.  
  138. }
  139.