home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / CustomerInfo.java < prev    next >
Encoding:
Java Source  |  1997-09-05  |  3.5 KB  |  128 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.     public boolean checkIfRegistered()
  17.     {
  18.         Request request = AspContext.getRequest();
  19.         Response response = AspContext.getResponse();
  20.         CookieDictionary cookDict = request.getCookies();
  21.         Cookie c = null;
  22.  
  23.         try 
  24.         {
  25.             c = cookDict.getCookie("CustInfo");
  26.         }
  27.  
  28.         catch (ClassCastException cce) 
  29.         {
  30.             response.write(INTERNAL_ERROR_MSG);
  31.             throw new RuntimeException();
  32.         }
  33.  
  34.         String strCookieValue = c.getValue();
  35.  
  36.         if (!strCookieValue.equals("")) 
  37.         {
  38.             // The customer has registered before.
  39.             return true;
  40.         }
  41.         else {
  42.             // The customer has NOT registered before.
  43.             return false;
  44.         }
  45.     }
  46.  
  47.     public boolean processForm()
  48.     {
  49.         Request request = AspContext.getRequest();
  50.         Response response = AspContext.getResponse();
  51.  
  52.         if (request.getTotalBytes() == 0) {
  53.             // No data here -- make the customer enter it
  54.             return false;
  55.         }
  56.  
  57.         CookieDictionary cookDict = request.getCookies();
  58.         RequestDictionary form = request.getForm();
  59.         Cookie cookCust = null;
  60.  
  61.         try 
  62.         {
  63.             cookCust = cookDict.getCookie("CustInfo");
  64.         }
  65.         catch (ClassCastException cce) 
  66.         {
  67.             response.write(INTERNAL_ERROR_MSG);
  68.             throw new RuntimeException();
  69.         }
  70.  
  71.         // Note: could return false if mandatory fields 
  72.         // aren't present
  73.         cookCust.setItem("Prefix", form.getString("Prefix"));
  74.         cookCust.setItem("FName",  form.getString("FName"));
  75.         cookCust.setItem("MName",  form.getString("MName"));
  76.         cookCust.setItem("LName",  form.getString("LName"));
  77.         cookCust.setItem("Suffix", form.getString("Suffix"));
  78.         cookCust.setItem("Addr1",  form.getString("Addr1"));
  79.         cookCust.setItem("Addr2",  form.getString("Addr2"));
  80.         cookCust.setItem("AptNo",  form.getString("AptNo"));
  81.         cookCust.setItem("City",   form.getString("City"));
  82.         cookCust.setItem("State",  form.getString("State"));
  83.         cookCust.setItem("ZIP",    form.getString("ZIP"));
  84.         cookCust.setItem("Birth",  form.getString("Birth"));
  85.         cookCust.setItem("SocSec",  form.getString("SocSec"));
  86.  
  87.         return true;
  88.     }
  89.  
  90.     public String getFullName()
  91.     {
  92.         Request request = AspContext.getRequest();
  93.         Response response = AspContext.getResponse();
  94.         CookieDictionary cookDict = request.getCookies();
  95.         RequestDictionary form = request.getForm();
  96.         Cookie cookCust = null;
  97.  
  98.         try 
  99.         {
  100.             cookCust = cookDict.getCookie("CustInfo");
  101.         }
  102.  
  103.         catch (ClassCastException cce) 
  104.         {
  105.             response.write(INTERNAL_ERROR_MSG);
  106.             throw new RuntimeException();
  107.         }
  108.  
  109.         // Note: Framework returns an empty string if the item isn't 
  110.         // found in the cookie
  111.  
  112.         String Prefix = cookCust.getItem("Prefix");
  113.         String FName = cookCust.getItem("FName");
  114.         String MName = cookCust.getItem("MName");
  115.         String LName = cookCust.getItem("LName");
  116.         String Suffix = cookCust.getItem("Suffix");
  117.  
  118.         String fullName = Prefix + (Prefix.equals("") ? "" : " ") + 
  119.                    FName + (FName.equals("") ? "" : " ") +
  120.                    MName + (MName.equals("") ? "" : " ") +
  121.                    LName + (LName.equals("") ? "" : " ") +
  122.                    Suffix;
  123.  
  124.         return(fullName);
  125.     }
  126.  
  127. }
  128.