🧠 Simple Calculator Application Using Servlet | TYBScIT Sem 5 Practical
🎓 If you're learning Advanced Java or preparing for your TYBScIT Semester 5 practicals, this guide walks you through building a Servlet-based calculator application — step by step. The blog is based on a real-time screen recording demo and covers essential advanced programming in Java.
✅ What You Will Learn:
- Create a Java Web Application in NetBeans
- Use HTML forms to collect user input
- Handle form data using a Java Servlet
- Deploy on GlassFish Server
- Output calculation results on a web browser
📽️ Step-by-Step Process (As Shown in the Video)
1️⃣ Create a Web Application Project
- Open NetBeans IDE 8.2
- Go to: File → New Project
- Choose: Java Web → Web Application
- Name the project (e.g., firstapp)
- Select GlassFish Server and Java EE 7 Web
2️⃣ Design the Frontend (index.html)
Create the following form using HTML:
🌐 HTML
<head><title>Calculator App</title></head><body>
<form action="CalculatorServlet" ><br>
Enter First Number <input type="text" name="txtN1"><br>
Enter Second Number <input type="text" name="txtN2"><br>
Select an Operation<br>
<input type="radio" name="opr" value="+">ADDITION<br>
<input type="radio" name="opr" value="-">SUBTRACTION<br>
<input type="radio" name="opr" value="*">MULTIPLY<br>
<input type="radio" name="opr" value="/">DIVIDE<br><br>
<input type="reset">
<input type="submit" value="Calculate"><br>
</form></body></html>
🌐 JAVA
package mypack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CalculatorServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Servlet CalculatorServlet</title></head><body>");
double n1 = Double.parseDouble(request.getParameter("txtN1"));
double n2 = Double.parseDouble(request.getParameter("txtN2"));
double result =0;
String opr=request.getParameter("opr");
if(opr.equals("+")) result=n1+n2; if(opr.equals("-")) result=n1-n2;
if(opr.equals("*")) result=n1*n2; if(opr.equals("/")) result=n1/n2;
out.println("<h1> Result = "+result); out.println("</body></html>");} }
4️⃣ Deploy and Run the Project
- Right-click the project → Clean and Build
- Then right-click → Run
- Visit: http://localhost:8080/firstapp/index.html
- Test inputs and operations in the browser
📘 Conclusion
This advanced java tutorial is perfect for TYBScIT Sem 5 students or anyone learning servlet-based Java applications. It teaches the fundamentals of advanced java concepts like servlets, form data handling, and server response logic.
If you're working on a final-year advanced java program, this is a great starting point.