Web 5
Web 5
Aim :
To write java servlet programs for Shopping cart.
Algorithm:
STEP 1: Start the program.
STEP 2: Create the HTML form to collect input from the user (e.g., text fields, radio buttons).
STEP 3: Add a submit button to send the form data to the server.
STEP 4: Import necessary Java and servlet packages.
STEP 5: Create a class that extends `HttpServlet` and override the `doPost()` method.
STEP 6: Set the content type of the response to "text/html".
STEP 7: Fetch form parameters from the client request.
STEP 8: Check the correctness of each input parameter and calculate the score.
STEP 9: Display the result in an HTML format using the response writer.
STEP 10: Stop the program.
Program:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f8ff;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0px 4px 8px rgba(0, 0, 0,
0.1); padding: 40px;
width: 300px;
text-align:
center;
}
h2 {
color: #333333;
}
label {
color: #555555;
}
input[type="text"],
input[type="password"] { width: 100%;
padding:
10px; margin:
10px 0;
border: 1px solid
#cccccc; border-radius:
input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
width: 100%;
border-radius:
5px;
22UIT503- WEB PROGRAMMING LABORATORY
Dr.N.G.P.Institute of Reg no:710722104010
cursor: pointer;
font-size: 16px;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form action="LoginServlet" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
cart.html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart - Fruits</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 20px;
text-align: center;
}
h2 {
color: #4CAF50;
}
.item {
display: inline-block;
width: 150px;
margin: 20px; text-
align: center;
}
.item img { width:
100px; height:
100px;
border-radius: 10px; border:
2px solid #ddd;
}
.item input[type="checkbox"] {
margin-top: 10px;
}
input[type="submit"] { background-
color: #4CAF50; color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>Shopping Cart - Fruits</h2>
<form action="OrderServlet" method="post">
<h3>Select items:</h3>
<div class="item">
<img
src="https://t3.ftcdn.net/jpg/03/10/32/02/360_F_310320273_I9rR1l7918MJoZ0GRHGIBgZl9F9ShE
Xq.jpg" alt="Apple">
<br>
<label>
<input type="checkbox" name="item" value="Apple"> Apple
</label>
</div>
<div class="item">
<img
src="https://t3.ftcdn.net/jpg/03/10/32/02/360_F_310320273_I9rR1l7918MJoZ0GRHGIBgZl9F9ShE
Xq.jpg" alt="Banana">
<br>
<label>
<input type="checkbox" name="item" value="Banana"> Banana
</label>
</div>
<div class="item">
<img src="https://www.shutterstock.com/shutterstock/photos/2053015835/display_1500/
stock-photo- orange-with-sliced-and-green-leaves-isolated-on-white-background-2053015835.jpg"
alt="Orange">
<br>
<label>
<input type="checkbox" name="item" value="Orange"> Orange
</label>
</div>
<br><br>
<input type="submit" value="Checkout">
</form>
</body>
</html>
login.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password =
request.getParameter("password");
if(username.equals("admin") && password.equals("admin123")) {
response.sendRedirect("cart.jsp");
} else {
response.getWriter().println("Invalid username or password.");
}
}
}
order.java
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[] selectedItems = request.getParameterValues("item");
if (selectedItems != null && selectedItems.length > 0) {
try (Connection conn = DBConnection.getConnection()) {
String sql = "INSERT INTO orders (item) VALUES (?)";
PreparedStatement ps = conn.prepareStatement(sql);
for (String item : selectedItems) {
ps.setString(1, item);
ps.executeUpdate();
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<html><body style='background-color:#e6ffe6;
color:#008000;'>");
response.getWriter().println("<h2>Order placed successfully!</h2>");
response.getWriter().println("<p>Thank you for ordering the following items:</p>");
response.getWriter().println("<ul>");
for (String item : selectedItems) {
response.getWriter().println("<li>" + item + "</li>");
}
response.getWriter().println("</ul>");
response.getWriter().println("</body></html>");
} catch (SQLException e) {
e.printStackTrace();
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<html><body style='background-color:#ffe6e6;
color:#ff0000;'>");
response.getWriter().println("<h2>Failed to place the order.</h2>");
response.getWriter().println("</body></html>");
}
} else {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<html><body style='background-color:#fff0e6;
color:#ff8000;'>");
response.getWriter().println("<h2>No items selected.</h2>");
response.getWriter().println("</body></html>");
}
}
}
Output:
Login page:
Cart page:
Successful order:
Updated in database:
Result:
Thus, java servlet program for Shopping cart is successfully executed and verified.