Project for Internet&Application NTUA
To run the Bookstore project locally, please follow the steps below:
- Install Tomcat Server 8.5:
- Visit the Tomcat download page .
- Download the appropriate distribution package for your operating system.
- Follow the installation instructions provided for your platform.
- Install MariaDB Server 11:
- Visit the MariaDB download page .
- Download the MSI package for Windows.
- Run the downloaded MSI package and follow the installation instructions.
- Create the user "root" with the password "hallo21" if you dont want to have to change the credentials in the project.
- Launch the MariaDB Command Line Client or any other MySQL client of your choice.
- Connect to the MariaDB server using your credentials.
mysql -u root -p
- Create the Database with ```console CREATE DATABASE Bookstore;
4. Import the necessary tables and example data for the Bookstore project using the mysqldump tool in the bin folder of mariadb.
The datadump.sql can be found next to the bookstore archive
```console
mysqldump -u username -p new_database < data-dump.sql
4.2 Alternatively you can also create the neccessary tables manually
USE bookstore;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
CREATE TABLE books (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL,
cover_image VARCHAR(255),
price DECIMAL(10, 2) NOT NULL,
description TEXT,
quantity INT NOT NULL
);
CREATE TABLE shopping_cart (
user_id INT,
book_id INT,
quantity INT,
PRIMARY KEY (user_id, book_id),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (book_id) REFERENCES books (id)
);
CREATE TABLE placed_orders (
user_id INT(11) NOT NULL,
book_id INT(11) NOT NULL,
quantity INT(11) NOT NULL,
id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
KEY idx_book_id (book_id),
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (user_id),
CONSTRAINT fk_book_id FOREIGN KEY (book_id) REFERENCES books (book_id)
);
- Download the zip file Bookstore.zip rom the GitHub repository .
- Extract the downloaded project folder to a location on your computer.
- Open Eclipse IDE.
- Import the project into Eclipse:
- Select the project and click Run on server .
- Wait for the server to start and deploy the Bookstore web application.
- Eclipse will open a web browser with the Bookstore application running on http://localhost:8080/Bookstore.
- MariaDB Connector/J:
- Library: mariadb-java-client.jar
- Usage:
public abstract class JDBCConnection {
static String url = "jdbc:mariadb://localhost:3306/bookstore";
static String userdb = "root";
static String passworddb = "hallo21";
Connection con = null;
public static Connection getMariaDbConnection() throws SQLException {
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return DriverManager.getConnection(url, userdb, passworddb);
}
}
- Description: The MariaDB Connector/J is a Java database driver for connecting Java applications to the MariaDB database. It provides the necessary functionality to handle the database queries.
- Servlet API (from Tomcat 8.5):
- Library: servlet-api.jar
- Description: The Servlet API provides the necessary classes and interfaces for creating servlets and handling servlet requests and responses. It is required for developing Java web applications using servlet containers such as Apache Tomcat.
- Download: You can find the servlet file in the bin folder of your tomcat Installation
- GSON:
- Description: GSON is a Java library developed by Google for serializing and deserializing Java objects to JSON and vice versa. It provides a simple API for working with JSON data, making it easier to parse and generate JSON content in Java applications.
- Library: gson.jar
- Usage:
import com.google.gson.Gson; booksJson = new Gson().toJson(books);
- Bootstrap:
-
Description: Bootstrap is a popular CSS framework that provides pre-styled components, grids, and CSS classes for building responsive web pages.
-
Usage:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<label for="user" class="form-label">Username</label>
<input type="text" class="form-control" name="user" id="user" placeholder="Enter your username">
- Font Awesome 4:
- Description: Font Awesome is a font and icon toolkit that allows you to easily add scalable vector icons to your web projects.
- Usage:
'<button onclick=deleteFromBasket(' + book['id'] +
') class="btn btn-danger btn-sm rounded-0" type="button"><i class="fa fa-trash-o" style="font-size:32px;color:red"></i></button>';
- Website: Font Awesome
Please make sure to download the appropriate versions of these libraries. Include these java-libraries in your project's classpath and add them to web application's WEB-INF/lib folder if not aleady present.
-The schema can be deduced by the Create table statements from the installation.
- Certainly! Here is the consolidated list of API calls used to communicate with the backend in the provided code:
- API Call:
POST http://localhost:8080/Bookstore/ShoppingCartServlet
- Function:
addToBasket()
- Method: POST
- Headers:
- Content-Type: application/x-www-form-urlencoded
- Body:
- action: putToBasket
- bookId
- quantity
- API Call:
POST http://localhost:8080/Bookstore/ShoppingCartServlet
- Function:
updateBasketQuantity(changedQuantityField)
- Method: POST
- Headers:
- Content-Type: application/x-www-form-urlencoded
- Body:
- action: 'updateBasket'
- bookId: [extracted from changedQuantityField.id]
- quantity: [value from changedQuantityField.value]
- API Call:
POST http://localhost:8080/Bookstore/ShoppingCartServlet
- Function:
deleteFromBasket(bookId)
- Method: POST
- Headers:
- Content-Type: application/x-www-form-urlencoded
- Body:
- action: deleteFromBasket
- bookId
- API Call:
GET http://localhost:8080/Bookstore/ShoppingCartServlet
- Function:
loadshoppingCart()
- Method: GET
- API Call:
GET http://localhost:8080/Bookstore/BookListServlet
- Function:
loadshoppingCart()
- Method: GET
- API Call:
POST http://localhost:8080/Bookstore/BookListServlet
- Function:
loadFoundBooks()
- Method: POST
- Headers:
- Content-Type: application/x-www-form-urlencoded
- Body:
- searchWords:
- API Call:
GET http://localhost:8080/Bookstore/BookDetailPageServlet?bookID=<bookID>
- Function: Dynamically generated in the with JSP
- Method: GET
- Parameters:
- bookID: