Create a simple calculator application using servlet
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>");
}
}