Java - 3.2 A
Java - 3.2 A
Experiment 3.2
Name: Anurag UID: 21BCS1040
Branch: CSE Section: 605-A
Subject: Java Lab Subject Code: 21CSH-319
2. Objective: The objective of this program is to create an application that uses Java
Servlet and takes input from user to add, multiply or divide numbers. The
application then returns the output on the interface.
3. Code:
index.jsp:
<!DOCTYPE html>
<html>
<head>
<title>Math Operations</title>
</head>
<body>
<h2>Math Operations</h2>
<form action="calculate.jsp" method="post">
<input type="checkbox" name="operation" value="add" checked>Addition
<input type="checkbox" name="operation" value="subtract">Subtraction
<input type="checkbox" name="operation" value="multiply">Multiplication
<input type="checkbox" name="operation" value="divide">Division<br><br>
if ("add".equals(operation)) {
result = num1 + num2;
} else if ("subtract".equals(operation)) {
result = num1 - num2;
} else if ("multiply".equals(operation)) {
result = num1 * num2;
} else if ("divide".equals(operation)) {
if (num2 != 0) {
result = num1 / num2;
} else {
errorMessage = "Error: Cannot divide by zero.";
request.setAttribute("error", errorMessage);
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
%>
4. Output:
5. Learning Outcomes:
• Learned how to use Apache Tomcat to run JSP applications.
• Design and implement a dynamic JSP calculator application.
• Deploy and test the JSP calculator on a web server optimizing performance.