provide me simple code for this easily neatly all
the 3 codes sql, html, java
Here are clean and simple solutions for both exercises, including SQL, HTML, and Java Servlet
code for each.
1. Stationery Store Cost Calculation
SQL Code
No database is needed for this simple calculation task.
HTML Code (index.html)
<!DOCTYPE html>
<html>
<head>
<title>Stationery Store Bill Calculator</title>
</head>
<body>
<h2>Stationery Purchase</h2>
<form action="StationeryServlet" method="post">
<input type="submit" value="Calculate Bill">
</form>
</body>
</html>
Java Servlet Code (StationeryServlet.java)
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/StationeryServlet")
public class StationeryServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int papers = 24, pens = 3, pencils = 5;
int paperPrice = 5, penPrice = 15, pencilPrice = 2;
int total = papers * paperPrice + pens * penPrice + pencils * pencilPrice;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h3>Total amount required: $" + total + "</h3>");
out.println("</body></html>");
out.close();
}
}
2. Product Price Discount Application
SQL Code
CREATE DATABASE productDB;
USE productDB;
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
model_name VARCHAR(50),
price DOUBLE,
brand VARCHAR(50)
);
HTML Code (product.html)
<!DOCTYPE html>
<html>
<head>
<title>Product Discount Calculator</title>
</head>
<body>
<h2>Product Price Discount</h2>
<form action="ProductServlet" method="post">
Model Name: <input type="text" name="model_name" required><br><br>
Price: <input type="number" name="price" step="0.01" required><br><br>
Brand: <input type="text" name="brand" required><br><br>
<input type="submit" value="Calculate Discount">
</form>
</body>
</html>
Java Servlet Code (ProductServlet.java)
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String modelName = request.getParameter("model_name");
String brand = request.getParameter("brand");
double price = Double.parseDouble(request.getParameter("price"));
String message = "";
// Validate entries
if(modelName.isEmpty() || brand.isEmpty() || price < 0) {
message = "Invalid product details entered.";
} else {
// Discount logic
double discount = 0;
if(price > 10000) {
discount = 0.10;
} else if(price >= 1000 && price <= 10000) {
discount = 0.05;
}
double finalPrice = price - (price * discount);
message = "Model: " + modelName + "<br>Brand: " + brand +
"<br>Original Price: Rs." + price +
"<br>Discount Applied: " + (int)(discount * 100) + "%" +
"<br>Final Price: Rs." + finalPrice;
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h3>" + message + "</h3>");
out.println("</body></html>");
out.close();
}
}
How to Use
Place HTML files in your web app.
Map form actions to servlet URLs.
Deploy servlets in your Java EE environment (Tomcat/Netbeans).
For exercise 2, create the products table if you want to store entries in your database
(optional for just calculation).
These codes are easy to understand and run. Modify field names or add more validations as
needed for your project.
⁂
1. image.jpg