0% found this document useful (0 votes)
7 views

web 5

The document outlines the development of a shopping cart application for an herbal company using Java Servlets and JDBC. It includes an algorithm for creating the application, HTML code for the login and cart pages, and Java code for handling user login and order placement. The project successfully demonstrates the functionality of a shopping cart system with user authentication and database integration.

Uploaded by

vunhsiv47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

web 5

The document outlines the development of a shopping cart application for an herbal company using Java Servlets and JDBC. It includes an algorithm for creating the application, HTML code for the login and cart pages, and Java code for handling user login and order placement. The project successfully demonstrates the functionality of a shopping cart system with user authentication and database integration.

Uploaded by

vunhsiv47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

710722205036 NISHORE N

Ex.No.5 DEVELOP A SHOPPING CART APPLICATION


THAT CAN BE USED IN AN HERBAL COMPANY
USING JAVA SERVLETS AND JDBC

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;

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
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:
5px;
}
input[type="submit"]
{ background-color:
#4CAF50; color: white;
border: none;
padding: 10px;
width: 100%;
border-radius:
22UIT503/ WEB PROGRAMMING
LABORATORY
710722205036 NISHORE N
5px;

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
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;

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
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;
22UIT503/ WEB PROGRAMMING
LABORATORY
710722205036 NISHORE N
}
</style>

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
</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>

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
</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;

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
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;'>");

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
response.getWriter().println("<h2>Failed to place the order.</h2>");
response.getWriter().println("</body></html>");

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N
}
} 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

22UIT503/ WEB PROGRAMMING


LABORATORY
710722205036 NISHORE N

Successful Order

Updated in Database

Result
Thus, java servlet programs for Shopping cart is successfully executed and verified.

22UIT503/ WEB PROGRAMMING


LABORATORY

You might also like