Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

This chapter is from the book

Building Peachmail: Registration and Login

Joe's first action when he arrives at Peachmail is to register for an account. He is presented with a screen to enter his username, password, email address, POPServer, and SMTPServer. Once he enters this information, Peachmail processes the information by sending this data to the server. After Peachmail confirms his account, Joe proceeds to log in. Let's first look at how Flash MX will generate the data to send to Java, and then explore how Flash handles the responses from Java.

The Front-End: Flash MX

Peachmail's first screen (Figure 7.10) contains two buttons: Login and New User.

Joe clicks on the New User button and is presented with the Create New User screen. He fills out his details and clicks Create, which triggers Flash to generate the following XML transaction data:

<Request>
  <TransactionType>
   CreateUser
  </TransactionType>
  <Data>
   <Username>
     myjoe
   </Username>
   <Password>
     myjoepassword
   </Password>
   <EmailAddress>
     joe@joes_email.com
   </EmailAddress>
   <POPServer>
     mail.joes_email.com
   </POPServer>
   <SMTPServer>
     smtp.joes_email.com
   </SMTPServer>
  </Data>
</Request>

Figure 7.10Figure 7.10 Peachmail's Login screen.


This is all the information that Peachmail needs to process the registration. Next, Joe is told that his registration is successful and he returns to the main screen and clicks Login. This triggers Flash to generate the login XML transaction data:

<Request>
   <TransactionType>
     Login
   </TransactionType>
   <Data>
     <Username>
        myjoe
     </Username>
     <Password>
        myjoepassword
     </Password>
   </Data>
</Request>

In the Peachmail application are functions named createHandler and loginHandler for the Create button and Login button, respectively. These button handlers help to generate the preceding XML documents, and pass them to Java for processing.

createHandler does the following:

  1. Creates the XML new user request from the input fields on the screen.

  2. Passes the request to Java.

  3. Creates a callback function to test whether the creation was successful.

Here is the ActionScript code for createHandler:

createHandler = function () {
   message.text = "Creating...";
   // check the two passwords
   if (password.text === passwordRepeat.text) {
     // make the xml
     var createUserXml = buildCreateUserRequest
         (username.text, password.text, email.text,
         popServer.text, smtpServer.text);
     // callback
     createUserCallback = function () {
        if (wasSuccessful(this.getDocIn())) {
          // goto the created interface
          gotoAndStop("created");
        } else {
          // show message
          message.text = getError(this.getDocIn());
        }
     }
     // send create user xml
     sendToServer(createUserXml, createUserCallback);
   } else {
     // make message
     message.text = "Passwords do not match";
   }
}

loginHandler does the following:

  1. Creates the XML login request from the username and password.

  2. Sends the data to Java for processing.

  3. Sets up a callback function to check whether the login was successful Here is the ActionScript code for loginHandler:

loginHandler = function () {
   // make the xml
   var loginXML = buildLoginRequest(username.text,
    password.text);
   // give a message
   message.text = "Logging In...";
   // check received xml
   loginCallback = function () {
     if (wasSuccessful(this.getDocIn())) {
        // goto email interface
        gotoAndStop("email");
     }else {
        // show error
        message.text = getError(this.getDocIn());
     }
   }
   // send login request
   sendToServer(loginXML, loginCallback);
} 

Notice how the loginHandler and the createHandler act similarly—both create a XML request, pass the request to Java, and create a callback function for the Java response, to see whether the request was successful. This create-send-callback action is used in other handlers throughout the Peachmail application.

buildBaseRequest: Creating the XML Request

loginHandler calls a function buildLoginRequest and createHandler calls a function buildCreateUserRequest. Let's take a quick look at the code:

buildLoginRequest = function (username, password) {
   // build the request
   var createLoginRequest = new buildBaserequest("Login");
   // add the username
   createLoginRequest.addData("Username", username);
   // add the password
   createLoginRequest.addData("Password", password);
   // return the finished xml doc
   return createLoginRequest.getDoc();
}
buildCreateUserRequest = function (username, password,
 emailAddress, POPServer, SMTPServer) {
    // Build a basic create user transaction
   var createCreateUserRequest = new buildBaseRequest(
   "CreateUser" );
   // add the username data to the xml doc
   createCreateUserRequest.addData("Username", username);
   // add the password data to the xml doc
   createCreateUserRequest.addData("Password", password);
   // add the email address
   createCreateUserRequest.addData("EmailAddress",
    emailAddress);
   // add the servers
   createCreateUserRequest.addData("POPServer", POPServer);
   createCreateUserRequest.addData("SMTPServer", SMTPServer);
   // return the finished XML document
   return createCreateUserRequest.getDoc();
} 

As you can see, both buildLoginRequest and buildCreateUserRequest create a buildBaseRequest object to form the proper XML document for the respective handler. Along with the addData() method, it creates the XML document mirroring the syntax of the two XML documents Joe created previously, and it returns the XML document to the respective handler.

Here is the constructor for buildBaseRequest and the addData method, along with some helper methods:

// buildBaseRequest constructor
function buildBaseRequest(transactionType) {
   // Create the XML Object
   this.doc = new XML();
   // Create the request node
   var requestNode = this.doc.createElement("Request");
   // Create the transaction node
   var transactionNode = this.doc.createElement
    ("TransactionType");
   // Create the transaction value
   var transactionNodeValue = this.doc.createTextNode
    (transactionType);
   // Populate the value of the transaction node
   transactionNode.appendChild(transactionNodeValue);
   // Create the data node
   var dataNode = this.doc.createElement("Data");
   // Append the transaction node to the request node
   requestNode.appendChild(transactionNode);
   // Append the data node to the request node
   requestNode.appendChild(dataNode);
   // Append the request node to the parent doc
   this.doc.appendChild(requestNode);
   // Return the completed document
   return this.doc;
} // end buildBaseRequest function

// addData function
buildBaseRequest.prototype.addData = function (newData,
 value, attributes) {
   // find the data node
   var dataNode = findDataNode(this.doc);
   // create the newData node
   var newDataNode = this.doc.createElement(newData);
   // check to see if a value was specified
   if (value != null)
     // create the value text node
     var valueTextNode = this.doc.createTextNode(value);
   // check to see if attributes were specified
   if (attributes != undefined)
     // loop through the attributes and set them
     for (var i in attributes)
    // add the attribute
     newDataNode.attributes[i] = attributes[i];
   // append valueTextNode to newDataNode
   newDataNode.appendChild(valueTextNode);
   // append newDataNode to dataNode
   dataNode.appendChild(newDataNode);
} // end adddata

// findDataNode function
// This helper function finds and returns the data node in a
 transaction xml doc
function findDataNode(xmlDoc) {
   // Get the children nodes
   var children = xmlDoc.firstChild.childNodes;
   // Get the second child node (the data node)
   var dataNode = children[1];
   // return the node
   return dataNode;
} // end findDataNode function

// getDoc function
// returns the xml doc
buildBaseRequest.prototype.getDoc = function () {
   // return the xml document
   return this.doc;
}

// wasSuccessful function
// This helper is used to determine if a transaction
 executed properly or not.
function wasSuccessful(xmlDoc) {
   // Create an XML object to be sure
   var xmlDoc = new XML(xmlDoc);
   // Parse through and find the status
   var mainNodes = xmlDoc.firstChild.childNodes;
   var statusNode = mainNodes[0];
   var status = statusNode.firstChild.nodeValue;
   // If its an error, find the message
   if(status == 'Error') {
     var dataNode = mainNodes[1];
     var messageNode = dataNode.firstChild;
     errorMessage = messageNode.firstChild.nodeValue;
     return false;
   }
   // return true if there was no error
   return true;
} // end wasSuccessful function

// getError function
// This helper is used to get the error of an unsuccesful
 xml doc
function getError(xmlDoc) {
   // Create an XML object to be sure
   var xmlDoc = new XML(xmlDoc);
   // Parse through and find the error
   var mainNodes = xmlDoc.firstChild.childNodes;
   var dataNode = mainNodes[1];
   var messageNode = dataNode.firstChild;
   errorMessage = messageNode.firstChild.nodeValue;
   // return error
   return errorMessage;
} // end getError function 

sendToServer: Sending XML to Java

Both loginHandler and createHandler use sendToServer() to send XML data to Java, like this:

sendToServer(loginXML, loginCallback); 

Here's a closer look at the sendToServer() function:

global.sendToServer = function (theXML, callBack) {
   // set the document to send to server
   server.setDocOut(theXML);
   // set the xml to recieve a reply from server
   server.setDocIn(new XML());
   // get the callback arguments from the arguments
   server.callBackArgs = arguments.slice(2);
   // store the callback function
   server.callBack = callBack;
   // set the callback function
   server.onLoad = function (success) {
     // check to see if there is an incoming doc
     if (success) {
        // call the callback function with the arguments
        this.callBack.apply(this, this.callBackArgs);
     } else {
        // call the server error function onServerDisconnect();
     }
   }
   // send the document
   server.execute();
} 

To quickly summarize, the sendToServer() function takes an object named server and passes it the XML data and the callback function, and then tells server to send the data by calling server.execute(). server is an object of a custom-made class named Serverdata()—its purpose to handle the passing of the data to Java in one neat little package. The following code shows how server is created and its defaults set.

global.server = new ServerData();
// set the methods
server.setMethod(server.SEND_AND_LOAD);
// set the language
server.setLanguage(server.JAVA);
// this is the url of server
server.setURL(myurl); 

Without going into all the detail regarding ServerData(), we'll look at what ServerData.execute() uses to send the XML data to Java: the Flash XML.sendAndLoad() method. The specific line of code inside ServerData() looks like this:

docOut.sendAndLoad(url, docIn); 

where docOut is the XML data being sent to the Java server located at url, and docIn is the variable that contains the result passed back from Java once docOut is sent and processed.

Once a response is returned from Java and Flash receives the data result, Flash automatically runs docIn.onLoad(), which is set up in ServerData() to process the callback function originally set by the handler.

Callbacks: Verifying Data Requests

Let's revisit the code for loginHandler, specifically the declaration of its call-back function and the passing of this function to the ServerData() object:

loginCallback = function () {
   if (wasSuccessful(this.getDocIn())) {
     // goto email interface
     gotoAndStop("email");
   } else {
     // show error
     message.text = getError(this.getDocIn());
   }
}
// send login request
sendToServer(loginXML, loginCallback); 

The loginCallback() function is sent as the second parameter of sendTo Server, so the ServerData object can call loginCallback() automat-ically, upon receiving a response after Java processes the XML. Also notice that loginCallback's action resulting from a successful transaction is to take Joe into the main part of the application (the email screen); it displays an error message for unsuccessful transactions.

Now that we've discussed the create-send-callback method, let's move on to the server and look at how the back-end detects, processes, and responds to the Flash code.

The Back-End: Java

Recall how Flash sends the XML data to Java—it uses the ServerData() object and the standard XML.sendAndLoad() method, sending the XML data stored in docOut to the server:

docOut.sendAndLoad(url, docIn); 

Also recall that docIn is the variable that will contain the result passed back from the server. How does this work? Flash automatically knows that by using the sendAndLoad() method, it will wait to receive data into docIn. Once the data is in docIn, Flash automatically executes the docIn.onLoad() method.

Running the docIn.onLoad() triggers the execution of the callback function. This is because we preset the docIn.onLoad() method to run the callback when sendToServer() is executed in the handler code:

sendToServer(loginXML, loginCallback); 

Processing the New User Request

Earlier you saw how Flash created the New User request when Joe clicked the Create button for the registration screen. When the Java server receives and processes the data, it returns to Flash a result indicating whether the process is successful, and as discussed earlier, this result is stored in the Flash variable docIn.

Java returns the following code to Flash when Java processes the data unsuccessfully:

<Response>
   <Status>
     Error
   </Status>
   <Data>
     <Message>
        Some message on why the new user request failed
     </Message>
   </Data>
</Response>

and successfully:

<Response>
   <Status>
     Success
   </Status>
   <Data>
     <Message>
        The transaction completed successfully
     </Message>
   </Data>
</Response>

Processing the Login Request

Much in the same way that new user requests are handled by Java, the login requests tell Java to generate the following code to send to Flash for an unsuccessful process:

<Response>
   <Status>
     Error
   </Status>
   <Data>
     <Message>
        Some message on why the login failed
     </Message>
   </Data>
</Response>

and for a successful process:

<Response>
   <Status>
     Success
   </Status>
   <Data>
     <Message>
        The transaction completed successfully
     </Message>
   </Data>
</Response>

The Database

On the create new user request, Joe's data updates the Users table of the database, as Java sends the information to the database to be written.

The login request triggers the Java server to allocate a thread for Joe, for handling Joe's connection throughout the life of the time he is logged into the application. The database is unaffected by Joe's login request.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020