Ex 9 : Three tire web application using JSP
Index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Online Shopping Form</title>
</head>
<body>
<h2>Online Shopping Order</h2>
<form action="register.jsp" method="post">
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" required><br><br>
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="product">Select Product:</label>
<select id="product" name="product" required>
<option value="">--Select--</option>
<option value="laptop">Laptop</option>
<option value="smartphone">Smartphone</option>
<option value="headphones">Headphones</option>
<option value="camera">Camera</option>
</select><br><br>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" value="1"
required><br><br>
<label for="payment">Payment Method:</label>
<select id="payment" name="payment" required>
<option value="">--Select--</option>
<option value="creditcard">Credit Card</option>
<option value="paypal">PayPal</option>
<option value="cod">Cash on Delivery</option>
</select><br><br>
<input type="submit" value="Place Order">
<input type="reset" value="Reset">
</form>
</body>
</html>
Register.jsp:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Order Result</title>
<style>
table {
border-collapse: collapse;
width: 50%;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<%
String fullname = request.getParameter("fullname");
String email = request.getParameter("email");
String product = request.getParameter("product");
String quantityStr = request.getParameter("quantity");
String payment = request.getParameter("payment");
int quantity = 0;
try {
quantity = Integer.parseInt(quantityStr);
} catch (NumberFormatException e) {
// Handle invalid quantity input
}
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1. Load JDBC driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
// 2. Establish connection
conn = DriverManager.getConnection(
"jdbc:derby://localhost:1527/userDB", "sonia", "sonia"); // Adjust DB name and
credentials
// 3. Prepare SQL INSERT query
String sql = "INSERT INTO online(FULLNAME, EMAIL, PRODUCT, QUANTITY, PAYMENT)
VALUES(?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fullname);
pstmt.setString(2, email);
pstmt.setString(3, product);
pstmt.setInt(4, quantity);
pstmt.setString(5, payment);
int rows = pstmt.executeUpdate();
if (rows > 0) {
%>
<h2>Order Placed Successfully!</h2>
<table>
<tr>
<th>Customer Name</th>
<td><%= fullname %></td>
</tr>
<tr>
<th>Email</th>
<td><%= email %></td>
</tr>
<tr>
<th>Product</th>
<td><%= product %></td>
</tr>
<tr>
<th>Quantity</th>
<td><%= quantity %></td>
</tr>
<tr>
<th>Payment Method</th>
<td><%= payment %></td>
</tr>
</table>
<%
} else {
%>
<h2>Order Failed!</h2>
<p>There was an issue processing your order. Please try again.</p>
<%
}
} catch (Exception e) {
out.println("<p style='color:red'>Error: " + e.getMessage() + "</p>");
} finally {
// 4. Close resources
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
out.println("<p style='color:red'>Error closing resources: " + e.getMessage() +
"</p>");
}
}
%>
<a href="index.html">Go Back to Shopping</a>
</body>
</html>
Output: