Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views4 pages

Practice Problem (Servlet)

The document provides clean and simple code solutions for two exercises: a Stationery Store Cost Calculation and a Product Price Discount Application. It includes SQL code for creating a database and table, HTML code for user input forms, and Java Servlet code for processing the calculations. Instructions for usage and deployment in a Java EE environment are also included.

Uploaded by

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

Practice Problem (Servlet)

The document provides clean and simple code solutions for two exercises: a Stationery Store Cost Calculation and a Product Price Discount Application. It includes SQL code for creating a database and table, HTML code for user input forms, and Java Servlet code for processing the calculations. Instructions for usage and deployment in a Java EE environment are also included.

Uploaded by

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

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

You might also like