DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment- 3.3
Student Name: Lakshay Sindhu UID: 21BCS10420
Branch: CSE Section/Group: 21BCS CC 628 - B
Semester: 6th (Sixth) Date of Performance: 04/04/24
Subject Name: Project Based Learning in Java Lab Subject Code: 21CSH-319
1. Aim : Create JSP application for addition, multiplication and division.
2. Objective : To learn about Java Server Pages JSP.
• To learn about various JSP tags.
3. Procedure :
1. Begin by initiating a Dynamic Web Project in Eclipse IDE.
2. Develop a JSP le within the project.
3. Launch the Tomcat server and deploy the project.
4. Initiate a Dynamic Web Project.
5. Develop a JSP le.
6. 6. Launch the Tomcat server and deploy the project..
4. Code and Output :
Program Code :
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h2>Simple Calculator</h2>
<label for="num1">Enter first number:</label>
fi
fi
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
<input type="number" id="num1" name="num1"><br><br>
<label for="num2">Enter second number:</label>
<input type="number" id="num2" name="num2"><br><br
<button onclick="add()">Add</button>
<button onclick="subtract()">Subtract</button>
<button onclick="multiply()">Multiply</button>
<button onclick="divide()">Divide</button><br><br>
<p id="result"></p>
<script> function add() {
var num1 = parseFloat(document.getElementById('num1').value);
var num2 = parseFloat(document.getElementById('num2').value);
var result = num1 + num2;
document.getElementById('result').innerHTML = 'Result: ' + result;
}
else {document.getElementById('result').innerHTML = 'Error: Division by zero!';
}}
</script>
</body>
</html>
JSP Code :
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CalculatorServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter(“num2”));
String operation = request.getParameter(“operation");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int result = 0;
if (operation.equals("add")) {
result = num1 + num2;
} else if (operation.equals("multiply")) {
result = num1 * num2;
} else if (operation.equals("divide")) {
if (num2 != 0) {
result = num1 / num2;
} else {
request.setAttribute("error", "Division by zero error!");
RequestDispatcher rd = request.getRequestDispatcher("calculator.jsp");
rd.forward(request, response);
return;
}
}
request.setAttribute("result", result);
RequestDispatcher rd = request.getRequestDispatcher("calculator.jsp");
rd.forward(request, response);
}
}
Output :
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Learning Outcomes :
1. Learnt about JSP(Java Server Pages) and Servlet in Java .
2. Learnt about servlet in java.
3. Learnt about managing data within web application.
4. Learnt about Client-side and Server-side form validation to ensure data integrity.