0% found this document useful (0 votes)
13 views

Create a simple calculator application using servlet

The document provides a simple calculator application using Java Servlets. It includes an HTML form for user input and a servlet (CalcServlet.java) that processes the input and performs basic arithmetic operations based on the user's selection. The servlet outputs the result of the calculation in HTML format.

Uploaded by

Jaya laxmi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Create a simple calculator application using servlet

The document provides a simple calculator application using Java Servlets. It includes an HTML form for user input and a servlet (CalcServlet.java) that processes the input and performs basic arithmetic operations based on the user's selection. The servlet outputs the result of the calculation in HTML format.

Uploaded by

Jaya laxmi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Create a simple calculator application using servlet.

index.html
<html>
<body>
<form method=post action="CalcServlet">
NO-1 <input type=text name="t1">
NO-2 <input type=text name="t2"> <br> <br>
<input type=submit value="+" name="btn">
<input type=submit value="-" name="btn">
<input type=submit value="*" name="btn">
<input type=submit value="/" name="btn">
</form>
</body>
</html>

CalcServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CalcServlet extends HttpServlet
{ public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{ response.setContentType("text/html");
PrintWriter out = response.getWriter();
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
int c=0;
String op=request.getParameter("btn");
if (op.equals("+"))
c=a+b;
else if (op.equals("-"))
c=a-b;
else if (op.equals("*"))
c=a*b;
else if (op.equals("/"))
c=a/b;
out.println("<b>"+a+op+b+" = "+c+"<b>");
}
}

You might also like