6.6. Web Component Client

Armed with the Customer (entity) EJB and CustomerSession (session) EJB, we now add login and sign up functionality to our Music Collection shopping application. Figure 6-8 depicts the web component's structure and the JSP files that supply the functionality. The new or updated components are white. (You may want to compare its structure to the previous version shown in Figure 5-6 on page 166.)

Figure 6-8. Relationship Among JSP Files After Adding Customer Database Verification and Creation


The sign up process requires files signUp.jsp to collect the new customer data and signUpPost.jsp to process it. The login page (file login.jsp) has an added link for new customers (its only change). The biggest modification from the version we presented in Chapter 5 is in loginPost.jsp, which now provides customer verification. These programs access the CustomerSession EJB to either create a new customer or verify an existing customer by name and password.

This section presents the sources for signUp.jsp, signUpPost.jsp, and loginPost.jsp. As you look through the code, note that accessing the Customer database is simple because we go through the session facade CustomerSession EJB. Let's start with login.jsp.

login.jsp

File login.jsp now has a link that accesses the new customer page (signUp.jsp). Since there are no other modifications, we display this link (shown in bold) between the last two lines of the code, as follows:

</form>
<br><br>If you are a new Customer
						<a href="signUp.jsp">Sign Up Here</a>
</body></html>

signUp.jsp

This JSP program creates an HTML table to help format the input form that asks for the required new customer information: name, password (typed in twice), and e-mail address. We use an HTML input form and specify page signUpPost.jsp as the action parameter. Listing 6.22 shows the source for signUp.jsp.

Listing 6.22. signUp.jsp
<%--
  signUp.jsp
--%>

<%--
  JSP Component to gather new Customer
  information.
--%>

<%@ page errorPage="error.jsp" %>

<%
  String requestURI = request.getRequestURI();
  session.putValue("signUpURL", requestURI);
%>
<html>
<head>
<title>New Customer Sign Up Page</title>
</head>
<body bgcolor=white>
<center>
<h1>Sign Up Page</h1>
<hr>
<p>
Please provide the following information
<form method="post" action="signUpPost.jsp">

<p>
<table border=2 cellpadding=5 bgcolor=#e0e0e0>
  <tr><td>
  Name:
  </td><td>
  <input TYPE="text" NAME="name" size=30>
  </td></tr>

  <tr><td>
  Password:
  </td><td>
  <input TYPE="password" NAME="password" size=30>
  </td></tr>

  <tr><td>
  Retype Password:
  </td><td>
  <input TYPE="password" NAME="password_2" size=30>
  </td></tr>

  <tr><td>
  Email:
  </td><td>
  <input TYPE="text" NAME="email" size=30>
  </td></tr>

  <tr><td align=center colspan=2>
  <input TYPE="submit" NAME="SignUp" VALUE=" Sign Me Up">
  <input TYPE="reset" VALUE=" Clear Form">
  </td></tr>
</table>
</form>
</body></html>

signUpPost.jsp

Listing 6.23 shows the source for signUpPost.jsp. This JSP program processes the new customer data provided by the user. It makes sure that none of the fields are empty and checks that both password fields are the same. If there are no problems, the program uses the CustomerSession EJB to create a new customer with the user-supplied information. If this succeeds (no exceptions are thrown), we display a successful login message and a link to enter the Music Collection site. Otherwise, we check the errorString variable. If there was a problem, errorString will be non-null. A text box appears with an error message and provides a link to the previous page, allowing the user to correct the problem.

Listing 6.23. signUpPost.jsp
<%--
  signUpPost.jsp
--%>

<%--
  JSP Web component to process new Customer
  information.
  Make sure user has supplied a value for
  each field.
--%>

<%@ page import="CustomerSession,CustomerSessionHome,CustomerVO,CustomerException
,CustomerIdentityException,java.util.*,
javax.ejb.CreateException, javax.naming.Context, javax.naming.InitialContext,
javax.rmi.PortableRemoteObject"
errorPage="error.jsp" %>

<%!
  CustomerSession mysession = null;
  public void jspInit() {
    try {
      Context initial = new InitialContext();
      Object objref = initial.lookup(
          "java:comp/env/ejb/CustomerSession");
      // get a home interface to CustomerSession EJB
      CustomerSessionHome home = (CustomerSessionHome)
          PortableRemoteObject.narrow(
          objref, CustomerSessionHome.class);
      // get a remote interface to CustomerSession EJB
      mysession = home.create();

    } catch (Exception ex) {
      System.out.println("Unexpected Exception: " +
          ex.getMessage());
    }
  }
%>

<%
  CustomerVO customer = null;
%>
<html>
<title>Process New Customer Information</title>
<body bgcolor=white>
<center>

<%
  String requestURI =
    (String)session.getValue("signUpURL");

  // Get name, password & email values provided by user
  String name = (String)request.getParameter("name");
  String password =
    (String)request.getParameter("password");
  String password2 =
    (String)request.getParameter("password_2");
  String email = (String)request.getParameter("email");

  // Check to make sure no fields are empty
  String errorString = null;
  if (name.equals("")
    || password.equals("") || email.equals("")) {
    errorString = "You have left a field empty. " +
      "You must provide a Name, " +
      "a Password, and an Email address.";
  }
  // make sure both password fields are the same
  else if (!password.equals(password2)) {
    errorString =
      "The passwords you provided were not the same. " +
      "Please try again.";
  }

  else { // everything is ok; create new customer
    try {
      customer = new CustomerVO(name, password, email);
      mysession.createCustomer(customer);
      session.putValue("customer", name);
      String newCustomer = "new";
      session.putValue("newCustomer", newCustomer);
%>

<h1>Login Successful</h1>
<p><h2>Welcome <%= customer.getName() %> </h2>
<hr><p>
<br><br><a href="musicCart.jsp">
Enter Music Collection Site</a>

<%
    } catch (CreateException ex) {
      errorString = ex.getMessage();
    }
  } // end else
  if (errorString != null) { // we have a problem

    // We use html formatting tags to create an error box
    // with red lettering so that it will stand out.

%>

<h1>Customer Information Error</h1>
<hr>
<p>
<p STYLE="background:#bfbfbf;color:red;
font-weight:bold;border:double thin;padding:5">
<%= errorString %>
</font>
<br><br><a href="<%= requestURI %>">
  Return to Sign Up Page</a>
<%
  } // end if
%>
</body></html>

Design Guideline

Recall that stateless session beans are shareable because they do not contain client-specific state information. In signUpPost.jsp, we can initialize CustomerSession variable mysession in a JSP declaration and share its instance with as many clients as necessary. We'll perform this same initialization in loginPost.jsp.


loginPost.jsp

The previous version of this web component requested customer login information, but only checked if the user-supplied fields were non-null. Any customer name and password were accepted. No actual customer was created and no information was saved. Since we've implemented CustomerSession EJB and Customer EJB, we can now create new customers and provide a login process that checks a user-supplied name and password with values stored in our customer database.

Listing 6.24 contains the source for file loginPost.jsp. This JSP program processes login information and verifies name and password fields with the underlying database by calling CustomerSession EJB's identifyCustomer(). You'll notice that its structure is very similar to signUpPost.jsp. Here, instead of creating a new customer record, we look up a preexisting record and verify that its name and password are correct.

Listing 6.24. loginPost.jsp
<%--
 loginPost.jsp
--%>

<%--
  JSP Web component to process login information.
  Make sure user has supplied a value for
  each field.
--%>
<%@ page import="CustomerSession,CustomerSessionHome,
CustomerException,CustomerIdentityException,java.util.*,
javax.naming.Context, javax.naming.InitialContext, javax.rmi.PortableRemoteObject"
errorPage="error.jsp" %>

<%!
  CustomerSession mysession = null;
  public void jspInit() {
    try {
      Context initial = new InitialContext();
      Object objref = initial.lookup(
          "java:comp/env/ejb/CustomerSession");
      // get a home interface to CustomerSession EJB
      CustomerSessionHome home = (CustomerSessionHome)
          PortableRemoteObject.narrow(
          objref, CustomerSessionHome.class);
      // get a remote interface to CustomerSession EJB
      mysession = home.create();

    } catch (Exception ex) {
      System.out.println("Unexpected Exception: " +
          ex.getMessage());
      ex.printStackTrace();
    }
  }
%>

<html>
<title>Process Login</title>
<body bgcolor=white>
<center>
<%
  String requestURI =
    (String)session.getValue("loginURL");

  // Get name and password values provided by user
  String name = (String)request.getParameter("name");
  String password =
    (String)request.getParameter("password");
  // Perform error checking on Customer input fields
  String errorString = null;
  // Check to make sure they're not empty
  if (name.equals("") || password.equals("")) {
    errorString = "You have left a field empty. " +
      "You must provide both a name " +
      "and a password.";
  }

  else {
    try {
      // see if name and password match
      mysession.identifyCustomer(name, password);
      session.putValue("customer", name);
      String newCustomer = "new";
      session.putValue("newCustomer", newCustomer);
%>

<h1>Login Successful</h1>
<p>
<h2>Welcome <%= name %> </h2>
<hr>
<p>
<br><br><a href="musicCart.jsp">
Enter Music Collection Site</a>

<%
    } catch (CustomerIdentityException ex) {
      errorString = ex.getMessage();
    }
  } // end else
  if (errorString != null) {         // we have a problem

    // We use html formatting tags to create an error box
    // with red lettering so that it will stand out.

%>

<h1>Login Error</h1>
<hr>
<p>
<p STYLE="background:#bfbfbf;color:red;
font-weight:bold;border:double thin;padding:5">
<%= errorString %>
</font>
<br><br><a href="<%= requestURI %>">
  Return to Login Page</a>

<%
  } // end if
%>
</body></html>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.35.58