User Interaction
User Interaction
The two forms should be configured to sent data typed towards the following addresses:
/customerCreation and /orderCreation. When setting up the two servlets, you should
establish a mapping respectively on each of the previously mentioned addresses in the
web.xml file;
The data shall be sent via the GET mothod of the HTTP protocol. The doGet() mothod
should be therefore implemented in your servlets;
The parameter names created during the transmission of data correspond to the values of
the attribute name of each <input> tag of the forms. For example, the customer name
being typed using the tag <input name="customerName".../> of the form, it shall be
accessible from the servlet using the following call:
request.getParameter("customerName ") ;
Hints on the model
Two entities are enough for this work, a customer and an order:
A bean Customer representing data collected from the form ceateCustomer.jsp (name,
firstName, etc);
A bean Order representing the data collected from the second part of the form
createOrder.jsp(date, amount, paymentMode etc…)
Concerning bean properties, it is advisable to declare them all as String, except the
amount of the order to be declared as double.
During the creation of an order, the user should type customer information. Instead of creating a
property for each field elated to customer in our customer bean, it will be suitable to include a
property customer which in its own will contain the properties name, firstName, etc…
Hints about controlers
The two forms created are configured to send the data typed to the server using the GET request. We
should therefore create two servlets called customerCreation and orderCreation which for each from
will:
Collect paraneters typed by calling the request.getParameter() on the various field names;
Will convert them in the suitable data types, then store them in their corresponding beans;
Verify if the user has forgotten to type required parameters (those with a star on the forms)
If yes, transfer the beans and an error message to a JSP for display
If not, transfer the beans and a successful message to a JSP for display
Since the date field shall be not activated, we should initialise the property date of the bean Order
with the current date. The data should therefore be handle as a string.
Hints about views
We need two form to inter data: createCustomer.jsp and createOrder.jsp. We also need to develop
two views: displayCustomer.jsp and displayOrder.jsp. These two views will receive one or
many beans and a message from their respective servlets and should display the data contain on
those objects. The EL expressions represent a wonderful opportunity to realise that.
SOLUTION
1. Project creation:
We create a project under eclipse using: file> new> Dynamic web project>Project name and enter tp1
then take next>next>click on Generate web.xml deployment descriptor>finish.
2. Views design
Go to the folder WebContent and create for JSP file as follows:
1-right click on WebContent folder>take new>take JSP file> in file name input createCustomer.jsp>take
next>finish and copy inside the following code after deleting all what was inside the new page created.
Code in createCustomer.jsp:
<%@ page pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Customer creation</title>
<link type="text/css" rel="stylesheet" href="inc/style.css" />
</head>
<body>
<div>
<form method="get" action=" customerCreation">
<fieldset>
<legend> Customer Informations</legend>
<label for="customerName">Name <span class="required">*</span></label>
<input type="text" id=" customerName" name=" customerName" value="" size="20"
maxlength="20" />
<br />
<label for="customerFirstname">First Name </label>
<input type="text" id=" customerFirstname " name=" customerFirstname " value="" size="20"
maxlength="20" />
<br />
<label for="customerAdress">Adress of delivery
<span class="required">*</span></label>
<input type="text" id=" customerAdress " name=" customerAdress " value="" size="20"
maxlength="20" />
<br />
<label for="customerPhone">Customer Phone Number
<span class="required">*</span></label>
<input type="text" id=" customerPhone " name=" customerPhone " value="" size="20"
maxlength="20" />
<br />
<label for="customerEmail">Adresse email</label>
<input type="email" id=" customerEmail " name=" customerEmail " value="" size="20"
maxlength="60" />
<br />
</fieldset>
<input type="submit" value="Validate" /><input type="reset" value="initialise to zero" /> <br/>
</form>
</div>
</body>
</html>
To test the following code do this: right click on tp1>Run as>on a server>next>add all>finish
Then go to your browser and type this as URL: http://localhost:8080/tp1/createCustomer.jsp. For now our
page does not look nice. For that reason let’s add the ccs code to arrange it.
To do that, right click on WebContent>take new>then take folder>input inc as folder name>take finish.
Now right click on inc> new>others>select CSS file and take next>input style.css>take next>finish. Copy
the following in the css file just created:
/* General ---------------------------------------------------------
---------------------------- */
body, p, legend, label, input {
font: normal 8pt verdana, helvetica, sans-serif;
}
/* Forms -----------------------------------------------------------
---------------------------- */
fieldset {
padding: 10px;
border: 1px #0568CD solid;
margin: 10px;
}
legend {
font-weight: bold;
color: #0568CD;
}
form label {
float: left;
width: 200px;
margin: 3px 0px 0px 0px;
}
form input {
margin: 3px 3px 0px 0px;
border: 1px #999 solid;
}
form input.sansLabel {
margin-left: 200px;
}
/* Color styles ----------------------------------------------
---------------------------- */
.required {
color: #c00;
}
.error {
color: #900;
}
.success {
color: #090;
}
.info {
font-style: italic;
color: #E8A22B;
}
This is what you obtained after typing the following URL on your browser:
http://localhost:8080/tp1/createCustomer.jsp