web applications: assignment

27
WEB APPLICATIONS: ASSIGNMENT By David Strugnell Student code: 0302564 Module code: EE2161 Assignment code: EE2161/1 Assignment name: Implementation Report

Upload: others

Post on 12-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WEB APPLICATIONS: ASSIGNMENT

WEB APPLICATIONS: ASSIGNMENT By David Strugnell Student code: 0302564 Module code: EE2161 Assignment code: EE2161/1 Assignment name: Implementation Report

Page 2: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 2

INTRODUCTION Using both java servlets and jsp pages I have produced a prototype of part of a online DVD store which would be used to demonstrate to the stakeholders how their system would work. The parts of the system that I have implemented are the basket page and the checkout page. This also required the creation of a products page, where the user can browse at the different DVDs, a way for the user to log in to his account and several pages in order to link all the pages together and to pass information between them. Java was used to create a java bean which where used to stored information about the DVDs that where added to the shopping basket. Java was also used to help generate an order conformation. All of the code that was written has been included in the appendices section at the end of this report. DESIGN OF THE IMPLEMENTATION In this section I will show the interactions between the users and the e-system. In order to access the system the user must input the correct URL into a web browser. When this is submitted the server dynamically generates the products page, shown in fig.1 below.

Fig. 1 - products.jsp

User: Clicks on one of the ‘Add to Basket’ buttons System: Uses ‘addtobasket.jsp’ page to add the selected DVD and its quantity to the basket. The user is then forwarded back to the products page.

Page 3: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 3

The links at the top of the page appear on every page that the user sees and therefore can be clicked at anytime during the use of the system. User: Clicks on ‘Home’ System: Forwards user to ‘products.jsp’ shown in fig.1 on the previous page. User: Clicks on ‘Basket’ System: Forwards user to ‘basket.jsp’ shown in fig. 2

Fig. 2 - Basket.jsp

User: Clicks on one of the remove buttons System: Uses ‘removefrombasket.jsp’ to remove the selected DVD from the basket and then forwards the user back to the basket. User: Clicks on ‘Checkout’ System: User is sent to the ‘checkout.jsp’ page, shown in fig. 5. If the user isn’t logged in they are forwarded to ‘login.jsp’, shown in fig. 3.

Fig. 3 - login.jsp

Page 4: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 4

User: Enters their username and password and then click ‘login’. System: Uses ‘processlogin.jsp’ to process the information entered. If the information is incorrect the user will is forwarded back to ‘login.jsp’ but this time the page will display an error message, as shown in fig. 4 below.

Fig. 4 - login.jsp (error)

System: When the information is correct ‘processlogin.jsp’ will forward the user to ‘checkout.jsp’, shown in fig. 5 below.

Fig. 5 - checkout.jsp

Page 5: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 5

User: clicks on ‘Update Details’ System: Forwards them to ‘update.jsp’ shown in fig.6 User: Can updates their details. User: Clicks on ‘Remove’ System: Uses ‘removefromcheckout.jsp’ to remove that DVD form the basket and then forwards the user back to the checkout page. User: Clicks the purchase button System: Uses ‘purchase.jsp’ to store the order details in a database, send the user a confirmation email, empty the basket and then forward the user back to the products page.

Fig. 6 - update.jsp

User: Can carry out any changes that they want to do. When they clicks on ‘Submit’. System: Saves details and forwards user to the checkout page. User: Clicks on cancel System: Any changes the user has made are forgotten and they are forwarded to the checkout page. The final link at the top is the exit link. User: Clicks on exit System: User’s current session is cleared and they are forwarded back to ‘products.jsp’. By clearing the session the shopping basket is cleared and the user is logged out of the system.

Page 6: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 6

IMPLEMENTATION products.jsp

This is a dynamically generated jsp page. The title and links that appear on every page that the user sees is added by importing a page called ‘navigation.jsp’. This was implemented by using the following code:

<jsp:include page="navigation.jsp"/>

The page then queries a table called ‘products’ from a the database using the following code:

<sql:query var="results"> select pid, name, image, price from products

</sql:query>

The page then iterates over each row, processes the data gathered and places that data into a HTML table. Each row from the database is given a separate row within the table. A ‘template’ for the table row is included within the jsp page in order to lay the data out in a specific way. Also added to the each row of the table is a HTML form that contains several hidden fields that contain the details about the DVD, a selection menu that allows the user to choose the number of DVDs they want to buy and a add to basket button. When the user clicks on ‘Add to Basket’ a page called ‘addtobasket.jsp’ processes that form. Before the addtobasket.jsp is used the URL is rewritten for when the user has cookies disabled on their browser. That data in the hidden fields is taken from the database during the same query as before and then passed into ‘addtobasket.jsp’ when the form is submitted. This saves time and money as the database only needs to be once rather than once on each page. The quantity selection menu is also produced dynamically in order to make it easier to update at a later stage. The following code is used to create the quantity selection menu:

Quantity:&nbsp; <select name="quantity">

<c:forEach begin="1" end="${ initParam.limit }" var="count"> <option value="${ count }"> ${ count } </option> </c:forEach>

</select>

Instead of inserting the end of the range of numbers here it takes it from the ‘web.xml’ file. With in the xml file the following code is inserted in order to make it work.

Page 7: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 7

<context-param> <param-name>limit</param-name> <param-value>10</param-value>

</context-param>

By changing the number between the param-value tags it is possible to change the range of with in the selection menu. addtobasket.jsp This page is used to process the request to add a DVD to the basket. It takes the data passed on from the ‘products.jsp’ and stores them in a java bean. An empty bean was created using java and is used a template for the individual DVD bean. This template was saved as DVD.class and was saved in folder called ee2161 within the classes folder. Before data can be saved within this bean the jsp page must call it by using the following code:

<jsp:useBean id="aDVD" class="ee2161.DVD">

The following ‘jsp:setProperty’ tag can then be used for each detail of the DVD in order to fill the bean in. The following code was used for the name of the DVD.

<jsp:setProperty name="aDVD" property="name" value="${param.name }" />

This bean is then added to a java vector that is available throughout the session under the name of shoppingBasket. This is done using the following code:

<jsp:useBean id="shoppingBasket" scope="session" class="java.util.Vector" /> <% shoppingBasket.add(aDVD); %>

The user is then forwarded back to the product page again rewriting the URL for people with cookies switched off. This was done using the following code:

<c:url value="products.jsp" var="products"/> <jsp:forward page="${products}" />

basket.jsp The basket page starts off by seeing if the basket has anything in or not. If it doesn’t it tells the user the their basket is empty. This was done by using the following code:

<c:if test="${empty sessionScope.shoppingBasket}"> <p>&nbsp;&nbsp;&nbsp;Your shopping basket is empty</p>

</c:if>

Page 8: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 8

If the basket has any DVDs in it then ‘basket.jsp’ iterates over the vector and creates a table with each bean making up a separate row. Each row also contains a form to allow the DVDs to be removed from the basket. The form contains a hidden field and a submit button. In order to process the form the server uses ‘removefrombasket.jsp’. The hidden field is used to pass on the position of the bean within the vector. In order to do the iteration I used the following tag:

<c:forEach var="dvd" items="${ sessionScope.shoppingBasket }" varStatus="status">

The code for the form is as follows:

<form action="${ remove }" method="post"> <input name="index" type="hidden" value="${ status.index }"> <input name="Submit" type="submit" value="Remove"> </td>

</form>

removefrombasket.jsp ‘Removefrombasket.jsp’ takes the index that was past to it from the basket page and changes it in to an integer called position. It then removes the bean from the vector that is at that position. This is done by using the following code:

<jsp:useBean id="shoppingBasket" scope="session" class="java.util.Vector" /> <% String index = request.getParameter( "index" ); int position = Integer.parseInt( index ); shoppingBasket.remove( position ); %>

The page then forwards the user back to the updated basket page. checkout.jsp

The first thing this page does is to check to see if the user is already logged in. It dose this by looking in to the session for something called with the id username which will only exist if the user is logged in. Therefore the user is forwarded to the login page. This is done using the following code:

<c:if test="${ empty sessionScope.username }"> <jsp:forward page="login.jsp" /> </c:if>

If the page finds that the user is logged in then it carries on generating the page. The first thing it must generate is the user details. This is done by querying a table within

Page 9: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 9

the database called users. This data is used to fill a HTML as well as set the values of several hidden fields in a form that follows the users details. The form also contains a submit button labelled update details which takes the user to an update page, ‘update.jsp’. After the user details the contents of the users basket must be generated. This is generated in the same way as the basket page except a page called ‘removefromcheckout.jsp’ is used to process the form instead of ‘removefrombasket.jsp’. The only difference is that ‘removefrombasket.jsp’ forwards the user to the checkout rather than the basket page. The final step in the checkout page is the purchase form. This is a form that contains several hidden field, whose values come from the database query done earlier in this page, and a submit button. A page called ‘purchase.jsp’ is used to process this form. login.jsp This page contains a HTML form with two input boxes for the user to put their username and password. Above this is a sentence asking the user to enter their username and password. When the form in submitted it is processed by ‘processlogin.jsp’. If the user’s details are incorrect they are sent back to the login page but this time it contains a error message. This is done by using the following tags:

<c:choose> <c:when test=> </c:when> <c:otherwise> </c:otherwise> </c:choose>

processlogin.jsp This page searches the user table in the database for the details submitted by the user. This is done by using the following code:

<sql:query var="results"> select * from users where username = ? and password = ?

<sql:param value="${ param.username }"/> <sql:param value="${ param.password }"/> </sql:query>

If the details are found then the following code is used to but the username into the session:

Page 10: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 10

<% String username = request.getParameter( "username" ); session.setAttribute( "username", username ); %>

The user is then forwarded to ‘checkout.jsp’. If the user’s details aren’t found an error is thrown and they are sent back to the login page. update.jsp This page is a HTML form is filled in with details that the system already has about the user. This allows the user to change just the details they want without having to rewrite all of his details. If the user clicks on cancel then ‘cancelupdate.jsp’ is used to send the user back to the checkout page without changing their details. If the user clicks submit then ‘doupdate.jsp’ is used to update the users details. doupdate.jsp This page takes all of the detail that were passed to it from the update page, trims them in order to get rid of white space at either end and then put the new details in to the database. In order to update the database the following code can be used:

<sql:update> update users set house=?, street=?, town=?, postcode=?, phone=?, mobile=?, email=? where username=?

This is followed by a number of the following tags each with a different property. The following tag is for the house details.

<sql:param value="${ fn:trim( param.house ) }"/>

purchase.jsp

The first thing this page does is to add the details each of the products ordered along with the details about the user and when the order was placed to a table in the database called purchases. This information is gathered from both the shoppingbasket vector that is store in the session as well as data that was passed to this page from the checkout via the hidden fields in the form. Each product is added put in a separate row of the database. The next task this page accomplishes is to send a confirmation email to the user. This is done by sending a set of parameters to a handler called ‘orderAck’ within the java class file called Utils.class. OrderAck then process these parameters and produces a message that is then emailed to the users email address. This page then clears the shopping basket as the user has now bought everything. The following code performs this operation:

Page 11: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 11

<% shoppingBasket.removeAllElements(); %>

The user is then forwarded back to ‘products.jsp’. exit.jsp This page clears the session and therefore logs the user out by using the following code:

<% session.invalidate(); %> This page then forwards the user to ‘products.jsp’ CONCLUSION When I first started this assignment I didn’t really understand any of the technologies that were being shown to us. Due to this, when I first started this section of the assignment, I didn’t enjoy doing the work. This changed though as I worked through the assignment, as I began to understand what the different code did. This allowed me to adapt the code that we were shown in our lectures in order to make my DVD store slightly different. If I was to do it again I would like to concentrate more on the aesthetic side of the DVD store, as I believe I am now fairly comfortable with the code that I have used. I would also like to add a few other features that you find on many real online shops. These may include a smaller view of you shopping basket on the products page and a registration page so that new users could register. Both of these features are things that I feel I could do using what I have learnt during this module.

Page 12: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 12

APPENDICES Database ee2161.sql drop database ee2161; create database ee2161; use ee2161; create table users (username varchar(32) primary key, password varchar(32), name varchar(64), email varchar(64), house varchar(64), street varchar(64), town varchar(64), postcode varchar(8), phone varchar(16), mobile varchar(16)); insert into users values ('username1', 'password1', 'John Smith', '[email protected]', '6', 'Bosanquet Close', 'Uxbridge', 'UB8 3PE', '01234 987654', '07812345678'); insert into users values ('username2', 'password2', 'Bob Jones', '[email protected]', 'Brackenfield House', 'Duchy Road', 'Harrogate', 'HG3 9HY', '01423 135791', '07808641454'); insert into users values ('username3', 'password3', 'David Strugnell', '[email protected]', '12', 'Mallinson Oval', 'Harrogate', 'HG2 9HH', '01423 870680', '07814700506'); describe users; select * from users; create table products (pid varchar(10) primary key, name varchar(64), image varchar(30), price double); insert into products values ('1', 'School of Rock', 'school.jpg', 12.99); insert into products values ('2', 'Shark Tale', 'shark.jpg', 12.99); insert into products values ('3', 'Saw', 'saw.jpg', 12.99); insert into products values ('4', 'Hero', 'hero.jpg', 12.99); insert into products values ('5', 'Shaun of the Dead', 'shaun.jpg', 12.99); describe products; select * from products; create table purchases (username varchar(32), pid varchar(10), quantity int(11), price double, stamp timestamp); describe purchases;

Page 13: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 13

JSP Pages products.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Welcome to the No.1 DVD Store</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <jsp:include page="navigation.jsp"/> <font face="Geneva, Arial, Helvetica, sans-serif"> <sql:query var="results"> select pid, name, image, price from products </sql:query> <c:url value="addtobasket.jsp" var="addtobasket"/> <table border="0" cellpadding="5">

<tr> <td colspan="5"> <hr> </td>

</tr> <c:forEach var="row" items="${ results.rows }"> <form action="${addtobasket}" method="post">

<tr> <td align="center" valign="top">${ row.pid }<font face="Geneva, Arial, Helvetica, sans-serif">)</font> </td> <td align="center"><img src = "images/${ row.image }" alt "${ row.name }" width="90" /></td> <td align="center" valign="top"> <p align="left"><strong>Details</strong></p> <p align="left">${ row.name }<br> &pound;${ row.price }</p> </td> <td align="center">

<input name="pid" type="hidden" value="${ row.pid }"> <input name="name" type="hidden" value="${ row.name }"> <input name="price" type="hidden" value="${ row.price }"> <input name="image" type="hidden" value="${ row.image }"> Quantity:&nbsp; <select name="quantity"> <c:forEach begin="1" end="${ initParam.limit }" var="count"> <option value="${ count }"> ${ count } </option> </c:forEach> </select>

</td> <td>

<input name="Submit" type="submit" value="Add to Basket">

Page 14: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 14

</td> </form>

</tr> <tr>

<td colspan="5"> <hr> </td> </tr>

</c:forEach> </table> </font> </body> </html> addtobasket.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <jsp:useBean id="aDVD" class="ee2161.DVD"> <jsp:setProperty name="aDVD" property="pid" value="${ param.pid }" /> <jsp:setProperty name="aDVD" property="image" value="${ param.image }" /> <jsp:setProperty name="aDVD" property="name" value="${param.name }" /> <jsp:setProperty name="aDVD" property="price" value="${ param.price * param.quantity }" /> <jsp:setProperty name="aDVD" property="quantity" value="${param.quantity }" /> </jsp:useBean> <jsp:useBean id="shoppingBasket" scope="session" class="java.util.Vector" /> <% shoppingBasket.add(aDVD); %> <c:url value="products.jsp" var="products"/> <jsp:forward page="${products}" /> </body> </html>

Page 15: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 15

basket.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Welcome to the No.1 DVD Store</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <jsp:include page="navigation.jsp"/> <font face="Geneva, Arial, Helvetica, sans-serif"> <c:url value="removefrombasket.jsp" var="remove"/> <c:if test="${empty sessionScope.shoppingBasket}"> <p>&nbsp;&nbsp;&nbsp;Your shopping basket is empty</p> </c:if> <table cellpadding="5" border="0">

<tr> <td colspan="3"> <hr> </td>

</tr> <c:forEach var="dvd" items="${ sessionScope.shoppingBasket }" varStatus="status">

<tr>

<td align="center"><img src = "images/${ dvd.image }" alt "${ dvd.name }" width="90" /></td> <td align="center"><div align="left">${ dvd.name }<br>

Quantity:&nbsp;${ dvd.quantity } <br> Total Price:${ dvd.price }</div></td>

<td> <form action="${ remove }" method="post"> <input name="index" type="hidden" value="${ status.index }"> <input name="Submit" type="submit" value="Remove"> </form>

</td> </tr> <tr>

<td colspan="3"> <hr> </td> </tr>

</c:forEach> </table> </font> </body> </html>

Page 16: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 16

removefrombasket.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <jsp:useBean id="shoppingBasket" scope="session" class="java.util.Vector" /> <% String index = request.getParameter( "index" ); int position = Integer.parseInt( index ); shoppingBasket.remove( position ); %> <c:url value="basket.jsp" var="basket"/> <jsp:forward page="${ basket }" /> </body> </html> checkout.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <html> <head> <title>Checkout</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <font face="Geneva, Arial, Helvetica, sans-serif"> <c:if test="${ empty sessionScope.username }"> <jsp:forward page="login.jsp" /> </c:if> <jsp:include page="navigation.jsp"/> <c:url value="removefromcheckout.jsp" var="remove"/>

Page 17: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 17

<sql:query var="results"> select * from users where username = '${ sessionScope.username }' </sql:query> <table cellpadding="5" border="0">

<tr> <td colspan="2"><strong>Current details for ${results.rows[0].name}:</strong></td>

</tr> <tr>

<td><strong>House Number/Name:</strong></td> <td>${ results.rows[0].house }</td>

</tr> <tr>

<td><strong>Street:</strong></td> <td>${ results.rows[0].street }</td>

</tr> <tr>

<td><strong>Town:</strong></td> <td>${ results.rows[0].town }</td>

</tr> <tr>

<td><strong>Postcode:</strong></td> <td>${ results.rows[0].postcode }</td>

</tr> <tr>

<td><strong>Email:</strong></td> <td>${ results.rows[0].email }</td>

</tr> <tr>

<td><strong>Phone Number:</strong></td> <td>${ results.rows[0].phone }</td>

</tr> <tr>

<td><strong>Mobile Number:</strong></td> <td>${ results.rows[0].mobile }</td>

</tr> <tr>

<td colspan="2" align="left"> <c:url value="update.jsp" var="update"/> <form action="${ update }" method="post">

<input name="house" type="hidden" value="${ results.rows[0].house }"> <input name="street" type="hidden" value="${ results.rows[0].street }"> <input name="town" type="hidden" value="${ results.rows[0].town }"> <input name="postcode" type="hidden" value="${ results.rows[0].postcode }"> <input name="email" type="hidden" value="${ results.rows[0].email }"> <input name="phone" type="hidden" value="${ results.rows[0].phone }"> <input name="mobile" type="hidden" value="${ results.rows[0].mobile }"> <input name ="update" type="submit" value="Update Details">

Page 18: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 18

</form> </td>

</tr> </table> <c:if test="${empty sessionScope.shoppingBasket}">

<p>&nbsp;&nbsp;&nbsp;Your shopping basket is empty</p> </c:if> <table cellpadding="5" border="0">

<tr> <td colspan="3"> <hr> </td>

</tr> <c:forEach var="dvd" items="${ sessionScope.shoppingBasket }" varStatus="status">

<tr> <td align="center"><img src = "images/${ dvd.image }" alt "${ dvd.name }" width="90" /></td> <td align="center"><div align="left">${ dvd.name }<br>

Quantity:&nbsp;${ dvd.quantity } <br> Total Price:${ dvd.price }</div></td>

<td> <form action="${ remove }" method="post"> <input name="index" type="hidden" value="${ status.index }"> <input name="Submit" type="submit" value="Remove"> </form>

</td> </tr> <tr>

<td colspan="3"> <hr> </td> </tr> </c:forEach> <tr>

<td colspan="3" align="right"> <c:url value="purchase.jsp" var="purchase" /> <form method="post" action="${ purchase }">

<input name="name" type="hidden" value="${ results.rows[0].name} "> <input name="house" type="hidden" value="${ results.rows[0].house} "> <input name="street" type="hidden" value="${ results.rows[0].street} "> <input name="town" type="hidden" value="${ results.rows[0].town }"> <input name="postcode" type="hidden" value="${ results.rows[0].postcode }"> <input name="email" type="hidden" value="${ results.rows[0].email }"> <input name="submit" type="submit" value="Purchase">

</form> </td>

</tr> </table> </font> </body> </html>

Page 19: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 19

login.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Login Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <jsp:include page="navigation.jsp"/> <c:url value="processlogin.jsp" var="processlogin"/> <font face="Arial, Helvetica, sans-serif"> <c:url value="processlogin.jsp" var="processlogin"/> <form method="post" action="${processlogin}"> <table cellpadding="10", cellspacing="5">

<tr> <td colspan="2">

<c:choose> <c:when test="${ param.error == 'unknown' }"> <font color="#FF0000"> Your Username or Password has not been recognised please try again </font> </c:when> <c:otherwise> Please enter your username and password </c:otherwise> </c:choose>

</td> </tr> <tr>

<td>Username:</td> <td><input type="text" name="username" value="${param.username}"></td>

</tr> <tr>

<td>Password:</td> <td><input type="password" name="password"></td>

</tr> <tr>

<td><input name="Submit" type="submit" value="Login"></td> </tr>

</table> </form> </font> </body> </html>

Page 20: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 20

processlogin.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <sql:query var="results">

select * from users where username = ? and password = ? <sql:param value="${ param.username }"/> <sql:param value="${ param.password }"/>

</sql:query> <c:choose> <c:when test="${ results.rowCount > 0 }">

<% String username = request.getParameter( "username" ); session.setAttribute( "username", username ); %> <c:url value="checkout.jsp" var="checkout"/> <jsp:forward page="${ checkout }"/>

</c:when> <c:otherwise>

<c:url value="login.jsp" var="login"/> <jsp:forward page="${ login }"> <jsp:param name="error" value="unknown" />

</jsp:forward> </c:otherwise> </c:choose> </body> </html> update.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Update Details</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head>

Page 21: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 21

<body> <font face="Arial, Helvetica, sans-serif"> <jsp:include page="navigation.jsp"/> <c:url value="doupdate.jsp" var="doupdate"/> <c:url value="cancelupdate.jsp" var="cancelupdate"/> <form action="${ doupdate }" method="post";> <table cellpadding="5" border="0">

<tr> <td colspan="2"><strong>Please update your details below:</strong></td>

</tr> <tr>

<td><strong>House Number/Name:</strong></td> <td><input name="house" type="text" value="${ param.house }" size="32" ></td>

</tr> <tr>

<td><strong>Street:</strong></td> <td><input name="street" type="text" value="${ param.street }" size="32" ></td>

</tr> <tr>

<td><strong>Town:</strong></td> <td><input name="town" type="text" value="${ param.town }" size="32" ></td>

</tr> <tr>

<td><strong>Postcode:</strong></td> <td><input name="postcode" type="text" value="${ param.postcode }" size="32" ></td>

</tr> <tr>

<td><strong>Email:</strong></td> <td><input name="email" type="text" value="${ param.email }" size="32" ></td>

</tr> <tr>

<td><strong>Phone:</strong></td> <td><input name="phone" type="text" value="${ param.phone }" size="32" ></td>

</tr> <tr>

<td><strong>Mobile:</strong></td> <td><input name="mobile" type="text" value="${ param.mobile }" size="32" ></td>

</tr> <tr>

<td colspan="2"> <table>

<tr> <td><input name="doupdate" type="submit" value="Submit"></form></td> <td><form action="${cancelupdate}" method="post"><input name="cancelupdate" type="submit" value="Cancel"></form></td>

</tr>

Page 22: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 22

</table> </td>

</tr> </table> </font> </body> </html> doupdate.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <sql:update> update users set house=?, street=?, town=?, postcode=?, phone=?, mobile=?, email=? where username=? <sql:param value="${ fn:trim( param.house ) }"/> <sql:param value="${ fn:trim( param.street ) }"/> <sql:param value="${ fn:trim( param.town ) }"/> <sql:param value="${ fn:trim( param.postcode ) }"/> <sql:param value="${ fn:trim( param.phone) }"/> <sql:param value="${ fn:trim( param.mobile ) }"/> <sql:param value="${ fn:trim( param.email ) }"/> <sql:param value="${ sessionScope.username }"/> </sql:update> <c:url value="checkout.jsp" var="checkout"/> <jsp:forward page="${ checkout }" /> purchase.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="mt" uri="http://jakarta.apache.org/taglibs/mailer-1.1" %> <jsp:useBean id="shoppingBasket" scope="session" class="java.util.Vector" /> <jsp:useBean id="now" scope="page" class="java.util.Date" /> <c:forEach var="dvd" items="${ shoppingBasket }">

Page 23: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 23

<sql:update> insert into purchases values (?, ?, ?, ?, ?) <sql:param value="${ sessionScope.username }" /> <sql:param value="${ dvd.pid }" /> <sql:param value="${ dvd.quantity }" /> <sql:param value="${ dvd.price }" /> <sql:dateParam value="${ now }" type="timestamp"/> </sql:update> </c:forEach> <% String name = request.getParameter( "name" ); String house = request.getParameter( "house" ); String street = request.getParameter( "street" ); String town = request.getParameter( "town" ); String postcode = request.getParameter( "postcode" ); String message = ee2161.Utils.orderAck( shoppingBasket, name, house, street, town, postcode ); %> <mt:mail server="smtp.brunel.ac.uk"> <mt:setrecipient type="to"> <%= request.getParameter("email") %> </mt:setrecipient> <mt:from>[email protected]</mt:from> <mt:subject>Order Conformation</mt:subject> <mt:message><%= message %></mt:message> <mt:send/> </mt:mail> <% shoppingBasket.removeAllElements(); %> <c:url value="products.jsp" var="products"/> <jsp:forward page="${ products }" /> exit.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% session.invalidate(); %> <c:url value="products.jsp" var="products"/> <jsp:forward page="${ products }" />

Page 24: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 24

Java Code DVD.java package ee2161; public class DVD { private String pid; private String name; private String image; private double price; private int quantity; public DVD() { pid = ""; name= ""; image = ""; price = 0.0; quantity = 0; } public String getPid() { return pid; } public void setPid( String pid ) { this.pid = pid; } public String getName() { return name; } public void setName( String name ) { this.name = name; } public String getImage() { return image; } public void setImage( String image ) { this.image = image; }

Page 25: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 25

public double getPrice() { return price; } public void setPrice( double price ) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity( int quantity ) { this.quantity = quantity; } } Utils.java package ee2161; import java.util.Vector; public class Utils { public static String orderAck(Vector vec, String name, String house, String street, String town, String postcode) { double total = 0; String message = "Dear " + name + "\n\nThank you for Shopping with " + "The No.1 DVD Store\n\nYour order:\n"; // loop over vector for (int j = 0; j < vec.size(); ++j) { // get DVD object from vector DVD dvd = (DVD) vec.get(j); // process price double price = (Math.floor(dvd.getPrice() * 100) / 100); total += price; // add current DVD to message message += "\t" + dvd.getQuantity() + ( (dvd.getQuantity() > 1) ? " copies of " : " copy of ") + dvd.getName() + " at £" + price + "\n"; } // add total and shipping address

Page 26: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 26

message += "\n\tTotal price = £" + total; message += "\n\nHas been shipped to:\n"; message += "\t" + house + "\n" ; message += "\t" + street + "\n" ; message += "\t" + town + "\n"; message += "\t" + postcode + "\n"; message += "\n----------------------------------------------\n"; message += "\nThank you for shopping with The No.1 DVD Store\n"; message += "\nThe No.1 DVD Store Customer Services"; message += "\nThe No.1 DVD Store (UK)"; return message; } } XML Page web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>ee2161 Web Applications</display-name> <description> Servlet/JSP Project </description> <context-param> <param-name>limit</param-name> <param-value>10</param-value> </context-param> <context-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name> <param-

value>jdbc:mysql://localhost/ee2161,com.mysql.jdbc.Driver</param-value>

</context-param>

Page 27: WEB APPLICATIONS: ASSIGNMENT

Implementation Report - By David Strugnell (0302564) 27

<listener> <listener-class> ee2161.ContextListener </listener-class> </listener> <servlet> <servlet-name>Login</servlet-name> <servlet-class>ee2161.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> <session-config> <session-timeout> 20 </session-timeout> </session-config> </web-app>