0% found this document useful (0 votes)
30 views3 pages

Java - 3.2 A

This document describes a JSP application that performs basic math operations. It has an index page to select the operation and enter numbers. A calculate page performs the operation and displays the result or error. It uses Java code embedded in JSP pages and forwards to an error page if there is a problem.
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)
30 views3 pages

Java - 3.2 A

This document describes a JSP application that performs basic math operations. It has an index page to select the operation and enter numbers. A calculate page performs the operation and displays the result or error. It uses Java code embedded in JSP pages and forwards to an error page if there is a problem.
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.2
Name: Anurag UID: 21BCS1040
Branch: CSE Section: 605-A
Subject: Java Lab Subject Code: 21CSH-319

1. Aim: Create JSP application for addition, multiplication, and division.

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>

Enter first number: <input type="text" name="num1"><br><br>


Enter second number: <input type="text" name="num2"><br><br>

<input type="submit" value="Calculate">


</form>
</body>
</html>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
calculate.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
<h2>Result</h2>
<%
String operation = request.getParameter("operation");
double num1 = Double.parseDouble(request.getParameter("num1"));
double num2 = Double.parseDouble(request.getParameter("num2"));
double result = 0;
String errorMessage = null;

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);
}
}
%>

<% if (errorMessage == null) { %>


<p>Result of <%= operation %>: <%= result %></p>
<% } %>
</body>
</html>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>Error</h2>
<p><%= request.getAttribute("error") %></p>
</body>
</html>

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.

You might also like