WHAT'S NEW?

Servlet Application to perform simple calculator application

HTML FILE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
 <form method="POST" action="http://localhost:8080/ZWebApp/calc">
  Enter First Number : <input type="text" name="first">
  <br><br><br>
  Select Operator : <input type="radio" name="operator" value="add" selected=true>+ &nbsp;&nbsp;<input type="radio" name="operator" value="sub">-&nbsp;&nbsp;<input type="radio" name="operator" value="mul">*&nbsp;&nbsp;<input type="radio" name="operator" value="div">/&nbsp;&nbsp;<input type="radio" name="operator" value="mod">%
  <br><br><br>
  Enter Second Number : <input type="text" name="second">
<br><br><br>
<input type="submit" value="Perform Operation">
</form>
 </body>

</html>


SERVLET FILE

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Calculator extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}


public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
PrintWriter pw;
response.setContentType("text/html");
pw=response.getWriter();

int num1=Integer.parseInt(request.getParameter("first"));
int num2=Integer.parseInt(request.getParameter("second"));
String operator=request.getParameter("operator");

pw.println("<html><body>");
if (operator.equals("add"))
{
pw.println(""+num1+" + "+num2+" = "+(num1+num2));
}

else if (operator.equals("sub"))
{
pw.println(""+num1+" - "+num2+" = "+(num1-num2));
}

else if (operator.equals("mul"))
{
pw.println(""+num1+" * "+num2+" = "+(num1*num2));
}

else if (operator.equals("div"))
{
pw.println(""+num1+" / "+num2+" = "+(num1/num2));
}

else 
{
pw.println(""+num1+" % "+num2+" = "+(num1%num2));
}


pw.println("</body></html>");
pw.close();

}
}

24 comments: Leave Your Comments

  1. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    Java Courses in chennai

    ReplyDelete
  2. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
    click here to login
    click here 4th std
    click here icon
    click here to register
    click here to apply

    ReplyDelete
  3. Thank you for sharing this. It was helpful. Keep sharing such things.
    Also I like to play Nulls Royale which is more fun than any other game.

    ReplyDelete
  4. Yes these codes are really helpful for newcomers as well as experts. They save alot of time as well.

    ReplyDelete
    Replies
    1. Here is the site for recent report of the various codes and their compilations.

      Delete
  5. Thanks for the detailed article on this topic. I would like to see more such awesome articles from you. Download GBWhatsApp APK

    ReplyDelete
  6. Thanks for sharing this valuable piece of information. Keep sharing more such awesome articles in the future. Goodbye! Download FMWhatsApp APK

    ReplyDelete