Professional Documents
Culture Documents
This appendix explains the components in the Resort Rental application, which enables end users to register, view rental information, or book
rooms or suites in these resorts.
The middle tier components consist of classes, which encapsulate Java DataBase Connectivity (JDBC) logic required to access information
in database tables. The presentation tier components invoke the business methods of the session bean to view information about resorts and
book rooms or suites. The presentation tier and the middle tier components make use of value objects that represents database table or
information about the end user.
n index.jsp: Contains the home page for the Resort Rental application.
n newuser.jsp: Provides a form used by an end user to register on the Web site.
n registered.jsp: Displays a message when an end user successfully creates a new user id.
n wrongpassword.jsp: Displays a message when a registered end user submits a wrong password.
n displayresortdata.jsp: Displays information about rooms and rental rates and provides a form to rent a room or suite.
n LoginDataAccessor: Defines a data accessor class that encapsulates information in a customer table.
n ResortBeansLocator: Defines a naming service locator class that enables application components to perform a JNDI lookup in the
JBossNS.
Page 2 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
package controller;
import java.io.Serializable;
// Declare a public class and implement Serializable interface
public class Resorts implements Serializable
{
private String resortID;
private String resortName;
private String resortLocation;
public Resorts()
{
}
// Accessor and mutator methods
public String getResortID()
{
return resortID;
}
public void setResortID(String resortID)
{
this.resortID = resortID;
}
public String getResortName()
{
return resortName;
}
public void setResortName(String resortName)
{
this.resortName = resortName;
}
public String getResortLocation()
{
return resortLocation;
}
public void setResortLocation(String resortLocation)
{
this.resortLocation = resortLocation;
}
}
package controller;
// Import necessary package
import java.io.Serializable;
public class Packages implements Serializable
{
// Declaring variables
private String resortID;
Page 3 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
package controller;
// Import necessary packages
import java.io.Serializable;
public class Rates implements Serializable
{
// Declare variables
private String resortID;
private int luxury;
private int Presedential;
private int pentHouse;
// Default constructor
public Rates()
{
}
// Accessor and mutator methods
public String getResortID()
{
return resortID;
Page 4 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
}
public void setResortID(String resortID)
{
this.resortID = resortID;
}
public int getLuxury()
{
return luxury;
}
public void setLuxury(int luxury)
{
this.luxury = luxury;
}
public int getPresedential()
{
return Presedential;
}
public void setPresedential(int Presedential)
{
this.Presedential = Presedential;
}
public int getPentHouse()
{
return pentHouse;
}
public void setPentHouse(int pentHouse)
{
this.pentHouse = pentHouse;
}
}
package controller;
// Import necessary packages
import java.io.Serializable;
public class Customer implements Serializable
{
// Declaring variables
private String emailID;
private String password;
private String customerName;
private String address;
private String socialSecurityNumber;
// Create default constructor
public Customer()
{
}
// Create Customer object with all the data
public Customer(String emailID, String password, String customerName,
String address, String socialSecurityNumber)
{
this.emailID = emailID;
this.password = password;
this.customerName = customerName;
this.address = address;
this.socialSecurityNumber = socialSecurityNumber;
}
// Accessor and mutator method for member variables
public void setEmailID(String emailID)
{
this.emailID = emailID;
}
Page 5 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
package controller;
import java.io.Serializable;
// Declare the class as public and implement the Serializable interface
public class Bookings implements Serializable
{
// Declare the member attributes
private String emailID;
private String resortID;
private String bookingPersonName;
private int luxuryNos;
private int PresedentialNos;
private int penthouseNos;
private int days;
private int adults;
private int children;
// Declare a null parameter constructor
public Bookings()
{ }
// Declare an overloaded constructor
public Bookings(String emailID, String resortID, String bookingPersonName,
int luxuryNos, int PresedentialNos, int penthouseNos, int days,
Page 6 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
Page 7 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
}
public int getPenthouseNos()
{
return this.penthouseNos;
}
public int getDays()
{
return this.days;
}
public int getAdults()
{
return this.adults;
}
public int getChildren()
{
return this.children;
}
}
The above listing provides hyperlinks to invoke the ControllerServlet class. These links assign a query string value member to enable existing
customer logon and a value new to redirect the end user to a registration page.
Page 8 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
var valid=true;
if(a.emailID.value.length==0)
{
valid=false;
alert("Username cannot be left blank");
a.emailID.focus();
}
else if(a.password.value.length==0)
{
valid=false;
alert("Password cannot be left blank");
a.password.focus();
}
else if(a.customerName.value.length==0)
{
valid=false;
alert("Name cannot be left blank");
a.customerName.focus();
}
else if(a.address.value.length==0)
{
valid=false;
alert("Address cannot be left blank");
a.address.focus();
}
else if(a.socialSecurityNumber.value.length==0)
{
valid=false;
alert("Social Security Number cannot be left blank");
a.socialSecurityNumber.focus();
}
return valid;
}
-->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<FONT size="7" color="blue" face="Lucida Console">
<I>ABC</I> Holiday Resorts
<HR>
</FONT>
</CENTER>
<H3 ALIGN=center>New User Registration</H3>
<FORM ACTION="Controller?option=register" METHOD="post" onSubmit="return validate(this)">
<TABLE ALIGN=center WIDTH=75%>
<TR>
<TD>UserId</TD>
<TD><INPUT TYPE=text NAME="emailID"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD><INPUT TYPE=password NAME="password"></TD>
</TR>
<TR>
<TD>Name</TD>
<TD><INPUT TYPE=text NAME="customerName"></TD>
</TR>
<TR>
<TD>Address</TD>
<TD><INPUT TYPE=text NAME="address"></TD>
</TR>
<TR>
<TD>Social Security Number</TD>
<TD><INPUT TYPE=text NAME="socialSecurityNumber"></TD>
</TR>
<TR>
<TD><INPUT TYPE=hidden NAME=option VALUE="register">
<TD><INPUT TYPE=submit VALUE=SUBMIT>
Page 9 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
</TR>
</TABLE>
</FORM>
<A HREF="index.jsp"><font color=blue face="Comic Sans" >[Home]</FONT></A>
</BODY>
</HTML>
The above listing provides a form to accept a customer name, address, email ID, and social security number. Validations are also performed
for the input information. The application registers an end user with the specified information and creates a record for this end user.
The above listing shows a message and provides a link to the index.jsp page.
Page 10 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
</HEAD>
<BODY>
<CENTER>
<FONT size="7" color="blue" face="Lucida Console">
<I>ABC</I> Holiday Resorts
<HR>
</FONT>
</CENTER>
<CENTER>
<FORM METHOD="post" ACTION="Controller?option=validate" onSubmit="return validate(this)"
<TABLE border="0" cellspacing="6" >
<TR>
<TD>UserId</TD>
<TD><INPUT type="text" name="emailID" size="20"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD><INPUT type="password" name="password" size="20"></TD>
</TR>
<TR>
<TD>
<TD><INPUT type="submit" value="Login"></TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML>
The above listing shows code to specify an email ID, which is the username of a registered end user and a password. When the end user
submits this form, a query string value, validate, is passed to the ControllerServlet class.
The above listing displays an error message when an unknown end user tries to logon to the application. It provides a link to return to the home
page.
Page 11 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
</HEAD>
<TITLE>Message Page</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT size="7" color="blue" face="Lucida Console">
<I>ABC</I> Holiday Resorts
<HR>
</FONT>
</CENTER>
<H3>Wrong password !</H3>
<A HREF="index.jsp"><font color=blue face="Comic Sans" >[ Home ]</FONT></A>
</BODY>
</HTML>
The above listing shows code to displays a message, when an end user provides an incorrect password, and enables the end user to return to
the application home page.
The above listing shows the code to assign a query string value, displayall, to the ControllerServlet class. This query string is used to invoke
code to display links to all resorts.
Page 12 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
</CENTER>
<center>
<%
Vector resort = (Vector) request.getAttribute("resort");
String user=(String) request.getAttribute("emailID");
int sizeResort=resort.size();
if (sizeResort>0)
{
%>
<H3>Click on any one of the resorts</H3>
<%
for(int i=0;i<sizeResort;i++)
{
Resorts resorts=(Resorts) resort.get(i);
String key = resorts.getResortID();
String value = resorts.getResortName();
%>
<A HREF="Controller?option=dispResort
&emailID=<%=user%>&id=<%= key.trim() %>">
<%= value.trim() %></A><P>
<%
}
}
%>
</center>
</BODY>
</HTML>
The above listing shows the code to obtain a request attribute called resort. This attribute contains a Vector object that contains key-value
pairs of the ID and name of a resort. The ID is used to create an instance of a Java bean called Resorts. This class contains an accessor
method called getResorts(). This method is used to retrieve the names of the available resorts.
Page 13 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
valid=false;
alert("Days cannot be left blank.");
a.days.focus();
}else if(a.adults.value.length==0) {
valid=false;
alert("The Field cannot be left blank. Enter 0 if not required");
a.adults.focus();
}else if(a.children.value.length==0) {
valid=false;
alert("The Field cannot be left blank. Enter 0 if not required");
a.children.focus();
}
if(valid)
{
valid=isItNumeric(a)
}
return valid;
}
function isItNumeric(a)
{
var luxNos=a.luxuryNos.value;
var presNos=a.PresedentialNos.value;
var pentNos=a.penthouseNos.value;
var days=a.days.value;
var adults=a.adults.value;
var children=a.children.value;
if (isNaN (luxNos))
{
alert (" Invalid Number ");
a.luxuryNos.select();
return false;
}
if (isNaN (presNos))
{
alert (" Invalid Number ");
a.PresedentialNos.select();
return false;
}
if (isNaN (pentNos))
{
alert (" Invalid Number ");
a.penthouseNos.select();
return false;
}
if (isNaN (days))
{
alert (" Invalid Number ");
a.days.select();
return false;
}
if (isNaN (adults))
{
alert (" Invalid Number ");
a.adults.select();
return false;
}
if (isNaN (children))
{
alert (" Invalid Number ");
a.children.select();
return false;
}
}
-->
</script>
</HEAD>
<BODY>
<CENTER>
<FONT size="7" color="blue" face="Lucida Console">
Page 14 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
Page 15 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
<TR>
<TD>No of children</TD>
<TD><INPUT type="text" name="children" maxlength="3" size="10"></TD>
</TR>
<TR>
<TD><INPUT type="submit" value="BOOK RESORT"></TD>
</TR>
</TABLE>
</FORM>
<%
request.setAttribute("ResortDate",vector);
%>
</BODY>
</HTML>
The above listing creates instances of two Java bean classes, Packages and Rates. The Packages bean displays information about the
number of luxury rooms, penthouses, and presidential suites available in a resort. The Rates bean displays the rental rates for these rooms.
package controller;
// Import necessary packages
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ControllerServlet extends HttpServlet
{
private ServletConfig config;
// init method that gets config object
public void init(ServletConfig config)
throws ServletException
{
Page 16 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
super.init(config);
this.config=config;
}
// get method that receives request
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request,response);
}
// post method that receives request
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request,response);
}
// Method that delegates tasks to the helper class
private void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
/* All request parameters from the client is encapsulated in a
HashMap object and then sent to the helper class for providing
desired service. */
HashMap hmap=new HashMap();
Enumeration enum=request.getParameterNames();
while(enum.hasMoreElements())
{
String key=(String) enum.nextElement();
String value=request.getParameter(key);
hmap.put(key,value);
}
// Creating an object for helper class and passing the HashMap
// object to the helper method
ControllerHelper helper=new ControllerHelper(config);
HashMap returnMap = helper.processRequest(hmap);
String page = (String) returnMap.get("page");
String message = (String) returnMap.get("message");
Vector resort = (Vector) returnMap.get("resort");
if(resort != null)
{
request.setAttribute("resort",resort);
}
String emailID = (String)returnMap.get("emailID");
if(emailID != null)
{
request.setAttribute("emailID",emailID);
}
Integer cost = (Integer)returnMap.get("cost");
if(cost != null)
{
request.setAttribute("cost",cost);
}
forwardRequest(request,response,page);
}
// Local method that uses a RequestDispatcher object to forward
// requests and responses to the client
private void forwardRequest(HttpServletRequest request, HttpServletResponse response,
String page)
throws ServletException, IOException
{
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
dispatcher.forward(request,response);
}
}
The ControllerServlet class in the above listing contains doGet() and doPost() methods to process HTTP GET and POST request. This class
defines a method called processRequest that is invoked by the doGet() and doPost() methods. The processRequest method stores request
parameters in a HashMap object. This method then creates an instance of a helper class called ControllerHelper. The ControllerHelper class
maintains information about a redirection page, message, and resort in a HashMap. This HashMap is retrieved by the ControllerServlet to
Page 17 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
package controller;
// Import necessary packages
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.ServletConfig;
public class ControllerHelper
{
// Declare necessary variables
private HashMap hashMap;
private ServletConfig config;
private ResortBeansLocator locator = null;
private ResortSessionHome home = null;
ResortSession remote = null;
// Create a default no argument constructor
public ControllerHelper()
{
}
// Create a constructor that accepts config object
public ControllerHelper(ServletConfig config)
{
this.config=config;
}
// Method that provides appropriate service to provide to client based on request
public HashMap processRequest(HashMap hashMap)
{
this.hashMap = hashMap;
String page = null;
String message = null;
HashMap hmap = null;
// Get the type of service required by the client
String option =(String)hashMap.get("option");
getSessionJNDI();
try
{
hmap = new HashMap();
// If client is not a member, ask him to register first
if(option.equals("new"))
{
// Redirecting to register page
page = "/WEB-INF/newuser.jsp";
}
// If a client wants to register, get his details
else if(option.equals("register"))
{
// Retrieve client details
String emailID=(String) hashMap.get("emailID");
String password=(String) (String) hashMap.get("password");
String customerName= (String) hashMap.get("customerName");
String address=(String) hashMap.get("address");
String socialSecurityNumber=(String)hashMap.get("socialSecurityNumber");
// Create a Customer ValueObject
Customer customerVO = new Customer();
// Use ValueObject setter method to set client data
customerVO.setEmailID(emailID);
customerVO.setPassword(password);
customerVO.setCustomerName(customerName);
customerVO.setAddress(address);
customerVO.setSocialSecurityNumber(socialSecurityNumber);
Page 18 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
Page 19 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
page="/WEB-INF/displayresorts.jsp";
hmap.put("emailID",emailID);
hmap.put("resort",resortsAll);
}
// If client wants to look at all data for a particular resort
else if(option.equals("dispResort"))
{
String emailID = (String) hashMap.get("emailID");
// Invoke session beans business method
// Display details of a particular resort
String resortID = (String) hashMap.get("id");
// Display resort data by redirecting
page = "/WEB-INF/displayresortdata.jsp";
Vector resort = remote.displayResortData(resortID);
hmap.put("emailID",emailID);
hmap.put("resort",resort);
}
// If client wants to book a resort
else if(option.equals("book"))
{
// Get all booking information from HashMap
String emailID = (String) hashMap.get("emailID");
String resortID = (String) hashMap.get("id");
String bookingPersonName = (String)hashMap.get("bookingPersonName");
String luxuryNos = (String)hashMap.get("luxuryNos");
String PresedentialNos = (String)hashMap.get("PresedentialNos");
String penthouseNos = (String)hashMap.get("penthouseNos");
String days = (String)hashMap.get("days");
String adults = (String)hashMap.get("adults");
String children = (String)hashMap.get("children");
Bookings bookings = new Bookings();
bookings.setEmailID(emailID);
bookings.setResortID(resortID);
bookings.setBookingPersonName(bookingPersonName);
bookings.setLuxuryNos(Integer.parseInt(luxuryNos));
bookings.setPresedentialNos(Integer.parseInt(PresedentialNos));
bookings.setPenthouseNos(Integer.parseInt(penthouseNos));
bookings.setDays(Integer.parseInt(days));
bookings.setAdults(Integer.parseInt(adults));
bookings.setChildren(Integer.parseInt(children));
// Invoke session beans business method
// Display details of a particular resort
page="/WEB-INF/message.jsp";
int cost = remote.bookResort(bookings);
hmap.put("cost",new Integer(cost));
hmap.put("emailID",emailID);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
hmap.put("page",page);
hmap.put("message",message);
return hmap;
}
public void getSessionJNDI()
{
try
{
// Create an instance of session bean service locator
locator=ResortBeansLocator.getInstance();
// Get a reference to session bean's home object
home = (ResortSessionHome)
locator.getHome("ResortBean", ResortSessionHome.class);
// Get a reference to session bean's remote object
remote=(ResortSession)home.create();
}
catch(Exception e)
Page 20 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
{
System.out.println(e.getMessage());
}
}
}
The above listing shows the ControllerHelper class used to process requests received by the ControllerServlet class. The ControllerHelper
class contains methods that perform the following operations:
n Invokes the newuser.jsp page when the ControllerServlet assigns a request parameter value, new.
n Obtains the name, address, email ID, and social security number of a new customer and assigns these values to a Java bean class called
CustomerVO. The CustomerVO class encapsulates attributes that map to columns in a customer table and enables the application to
insert a record in this table.
n Uses a service locator class to obtain the home object reference of the session bean. This method then invokes the create method to
create a session bean instance.
package controller;
// Import necessary packages
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Vector;
public class LoginDataAccessor
{
private Connection con;
public LoginDataAccessor()
{
try
{
// Create an initial context for JDBC lookup, get reference to data source
// and get connection
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/AppendixDS");
this.con = ds.getConnection();
}
catch(NamingException e)
{
System.out.println(e.getMessage());
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
// Method that inserts client data in a database
public int insert(Customer customer)
{
int rowsInserted = 0;
PreparedStatement ps = null;
try
Page 21 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
{
ps = con.prepareStatement("INSERT INTO CustomerInfo VALUES(?,?,?,?,?)");
ps.setString(1,customer.getEmailID());
ps.setString(2,customer.getPassword());
ps.setString(3,customer.getCustomerName());
ps.setString(4,customer.getAddress());
ps.setString(5,customer.getSocialSecurityNumber());
rowsInserted = ps.executeUpdate();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
// Close prepared statement object
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
return rowsInserted;
}
public int validate(Customer customer)
{
// Method that validates a customer
/* This Method returns the following values
0 = username and password are correct
1 = username not found in table
2 = incorrect password. */
int result = 0;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
// Select email id and password from CustomerInfo table
ps = con.prepareStatement("SELECT emailID,password from CustomerInfo WHERE emailID=?");
ps.setString(1,customer.getEmailID());
rs = ps.executeQuery();
// If client email id found, there is one record in result set, then, if
// condition becomes true
if(rs.next())
{
// Get the password from the CustomerInfo table
String dbPassword = rs.getString("password");
// Check whether client's password is the same as that from
// database
if(dbPassword.equals(customer.getPassword()))
{
// If other email id and password is the same, display
System.out.println("UserName and password are correct ..");
}
else
{
// If password is wrong, set a value to a variable
result = 2;
}
}
else
{
// if given user is wrong, display message
// set a value to a result
Page 22 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
result = 1;
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
// close prepared statement
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
// return current value for the variable
return result;
}
// method that closes database connections
public void closeConnection()
{
try
{
if(this.con != null)
{
this.con.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}
n Connect to the database and inserts new records into the CustomerInfo table.
n Validate a login by retrieving information from the CustomerInfo table.
Page 23 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
Page 24 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
System.out.println(e.getMessage());
}
}
}
The above listing shows that the session bean object is located using the InitialContext object.
package controller;
// Import necessary packages
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Vector;
import java.io.Serializable;
public class ResortDataAccessor implements Serializable
{
private Connection con;
public ResortDataAccessor()
{
try
{
// Get a reference to initial context
InitialContext ctx = new InitialContext();
// Use context to look up a datasource
DataSource ds = (DataSource)ctx.lookup("java:/AppendixDS");
// Get connection to the data source
con = ds.getConnection();
}
catch(NamingException e)
{
System.out.println(e.getMessage());
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
public Vector displayResorts()
{
// Display all available resorts for rent
Vector result = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
// Select resort id and resort name for available resorts
ps = con.prepareStatement("SELECT resortID,resortName FROM Resorts");
rs = ps.executeQuery();
Page 25 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
if(rs == null)
{
// If resorts are not available
System.out.println("No resorts available for rent ... ");
}
else
{
// Create a Vector to hold Resorts ValueObject
result = new Vector();
while(rs.next())
{
// Create a Resorts ValueObject
// Set the resortID and resortName to the ValueObject
Resorts resorts = new Resorts();
String resortID = rs.getString("resortID");
String resortName = rs.getString("resortName");
resorts.setResortID(resortID);
resorts.setResortName(resortName);
// Add ValueObject to result Vector
result.add(resorts);
}
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
// Close result set and prepared statement objects
if(rs != null)
{
rs.close();
}
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage()); }
}
// Return the Vector containing Resorts ValueObject
return result;
}
// Create a method that displays data associated with a particular resort
public Vector displayResortData(String resortID)
{
// Declare necessary variables
PreparedStatement ps = null;
ResultSet rs = null;
// Create Packages and Rates ValueObject that store package and rates info about
// a particular resort
Packages packages = new Packages();
Rates rates = new Rates();
// Create a Vector object to store multiple ValueObjects
Vector vector = new Vector();
try
{
// Select the number of each available package
ps = con.prepareStatement("SELECT luxury, Presedential, pentHouse
from Packages WHERE resortID=?");
ps.setString(1,resortID);
rs = ps.executeQuery();
if(rs.next())
{
Page 26 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
Page 27 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
rs.close();
ps.close();
// Insert the client booking data in a Bookings table
String fields = "(emailID, bookingPerson, luxuryRooms,
PresedentialRooms, pentHouseRooms, numberOfDays, adults,
children, cost, resortID)";
ps = con.prepareStatement("INSERT INTO Bookings "+
fields+" VALUES(?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,emailID);
ps.setString(2,bookingPersonName);
ps.setInt(3,luxuryNos);
ps.setInt(4,PresedentialNos);
ps.setInt(5,penthouseNos);
ps.setInt(6,days);
ps.setInt(7,adults);
ps.setInt(8,children);
// Calculate cost for the customer, store in cost variable
int head = adults+children;
int cost = ((luxuryNos*luxRate) + (PresedentialNos*presRate) + (penthouseNos*pentRate))*
ps.setInt(9,cost);
ps.setString(10,resortID);
update = ps.executeUpdate();
System.out.println("Room booked for the client. Thank you ... ");
ps.close();
// Update Packages table to reduce rooms
ps=con.prepareStatement("update Packages set luxury=(luxury-?),Presedential=(Presedentia
pentHouse=(pentHouse-?) where resortID=?");
ps.setInt(1,luxuryNos);
ps.setInt(2,PresedentialNos);
ps.setInt(3,penthouseNos);
ps.setString(4,resortID);
int j = ps.executeUpdate();
totalCost=cost;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
// Close prepared statement object
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
// Return cost to the client
return totalCost;
}
public void closeConnection()
{
try
{
// Close connection
if(this.con != null)
{
con.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
Page 28 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
}
}
}
In the above listing, the DataSource class is used to lookup the database.
You create the home interface of the session bean to specify the create() method used to instantiate the bean. The home interface is created
by extending the EJBHome interface. Listing A-21 shows the home interface of the session bean:
package controller;
// import necessary packages
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ResortSessionHome extends EJBHome
{
// create method that returns a reference to remote object
public ResortSession create() throws RemoteException,CreateException;
}
You create the remote interface to expose the business methods of the session bean. These business methods are implemented in the bean
implementation class. You create the remote interface by extending the EJBObject. Listing A-22 shows the remote interface of the session
bean:
package controller;
// import necessary packages
import javax.ejb.EJBObject;
import javax.ejb.EJBException;
import java.rmi.RemoteException;
import java.util.*;
public interface ResortSession extends EJBObject
{
// declaring business methods
public Vector displayResorts() throws RemoteException;
public Vector displayResortData(String resortID) throws RemoteException;
public int bookResort(Bookings bookings) throws RemoteException;
}
You create the session bean implementation class to provide implementation for the business methods exposed by the remote interface. The
business methods of the session bean use the methods of the ResortDataAccessor class to obtain data from the database. These methods
return the database data in the form of value objects. Listing A-23 shows the implementation class of the session bean:
package controller;
// Import necessary packages
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
Page 29 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
import java.util.*;
public class ResortSessionBean implements SessionBean
{
// Declaring variables
private SessionContext context = null;
private ResortDataAccessor dao = null;
// default constructor
public ResortSessionBean()
{
}
// Implementation method that displays all the available resorts
public Vector displayResorts()
{
// Returns HashMap with String objects resortId and resortName
// as key - value mapping.
Vector result = null;
try
{
// Create data accessor object
dao = new ResortDataAccessor();
// Get all the available resorts as a Vector
result = dao.displayResorts();
// Close connection
dao.closeConnection();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return result;
}
// Implementation method that displays all the data associated with a particular resort
public Vector displayResortData(String resortID)
{
// Returns Vector object containing
/*
0 = resortId as String object
1 = Packages object containing
int rooms available
2 = Rates object containing
rate/room/person
*/
Vector result=null;
try
{
// Create data accessor object
dao = new ResortDataAccessor();
// Invoke method on data accessor to get all the data associated with
// individual resort
result = dao.displayResortData(resortID);
// Close connection
dao.closeConnection();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return result;
}
// Method to book a resort
public int bookResort(Bookings bookings)
{
int result=0;
try
{
// Create a data accessor value object
dao = new ResortDataAccessor();
// Invoke method on data accessor value object to insert booking data
result = dao.bookResort(bookings);
Page 30 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
// Close connection
dao.closeConnection();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return result;
}
// EJB callback methods
public void ejbCreate() throws CreateException
{
}
public void ejbRemove()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void setSessionContext(SessionContext sc)
{
this.context=sc;
}
}
Creating Tables
The login information of the end user is stored in the CustomerInfo database. This table is used in the Presentation tier of the application.
Table A-1 lists the structure of the CustomerInfo table:
Table A-1: Structure of the
CustomerInfo Table
Fieldname Datatype
EmailID varchar(40)
Password varchar(20)
CustomerName varchar(40)
Address varchar(100)
SocialSecurityNumber varchar(15)
The information about the location of various resorts is stored in the Resorts table. This table is used by the session been in the middle tier.
Table A-2 lists the structure of the Resorts table:
Table A-2: Structure of the
Resorts Table
Fieldname Datatype
ResortID varchar(4)
resortName varchar(30)
ResortLocation varchar(40)
The information about the rooms available in various resorts is stored in the Packages table. Table A-3 lists the structure of the Packages
table:
Table A-3: Structure of
the Packages Table
Fieldname Datatype
Page 31 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
ResortID varchar(4)
Luxury int(4)
Presidential int(4)
PentHouse int(4)
The information about the rates to be charged for the various rooms available is stored in the Rates table. Table A-4 lists the structure of the
Rates table:
Table A-4: Structure of
the Rates Table
Fieldname Datatype
ResortID varchar(4)
Luxury int(4)
Presidential int(4)
PentHouse int(4)
The information about the bookings made by the end user for a resort, is stored in the Bookings table. Table A-5 lists the structure of the
Bookings table:
Table A-5: Structure of the Bookings Table
Fieldname Datatype
bookID int() PRIMARY KEY AUTO_INCREMENT
resorted varchar(4)
emailed varchar(40)
bookingPerson varchar(40)
luxuryRooms int(4)
presidentialRooms int(4)
pentHouseRooms int(4)
numberOfDays int(4)
Adults int(4)
Children int(4)
Cost int(4)
Page 32 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
</servlet>
<servlet>
<servlet-name>displayresorts</servlet-name>
<jsp-file>/displayresorts.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>display</servlet-name>
<jsp-file>/display.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>displayresortdata</servlet-name>
<jsp-file>/displayresortdata.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
</web-app>
The deployment descriptor for the middle tier components is described in the ejb-jar.xml file. Listing A-25 shows the ejb-jar.xml file:
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<display-name>ResortJAR</display-name>
<enterprise-beans>
<session>
<ejb-name>ResortBean</ejb-name>
<home>controller.ResortSessionHome</home>
<remote>controller.ResortSession</remote>
<ejb-class>controller.ResortSessionBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
The presentation tier components must be deployed as a war file and the middle tier components must be deployed as a jar file. The jar file
and the war file are packaged together as an ear file for ease of deployment. The deployment descriptor for the ear file is described in
application.xml file. Listing A-26 shows the application.xml file:
Page 33 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
directory and place all Java files under a folder javaFiles in the source directory. XML and JSP pages need to be placed in the source
directory. Start the JBoss server before running the ANT tool. Listing A-27 shows the build.xml file used by this application:
To perform the ANT build, you need to change to the working directory where you have saved the build.xml file. Run the ANT tool from the
command line. Figure A-1 shows the output of the ANT tool:
Page 34 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
2. Click Sign Up to register to the Web site. The new user login page appears.
3. Specify the required information for user registration, as shown in Figure A-3:
Page 35 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
4. Click Submit. The information is sent to the servlet for processing and the result of the database operations is displayed to the end user,
as shown in Figure A-4:
Page 36 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
7. Click Login. The end user is authenticated and a hyperlink to view all resort location appears, as shown in Figure A-6:
8. Click Display Resorts hyperlink to view all resorts. Figure A-7 shows the output of the displayresorts.jsp file:
Page 37 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
9. Click East Resort hyperlink to view information about East Resort resort, as shown in Figure A-8:
10. Specify the information required to book a resort, as shown in Figure A-9:
Page 38 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited
Working with JBOSS Application Server
11. Click BOOK RESORT. The information is sent to the servlet and the total cost is calculated and returned to the end user, as shown in
Figure A-10:
Page 39 / 39
Reprinted for OET7P/751897, Accenture SkillSoft, SkillSoft Corporation (c) 2003, Copying Prohibited