SERVLET PROGRAM FOR BASIC ARITHMETIC OPERATIONS

Calc.html

<html>
<head>
</head>
<body>
<form method="get" action="calc3">
<h3>Enter First Number:<input type="text" name="fnum"></h3><br>

<h3>Enter Second Number:<input type="text" name="snum"></h3><br>
<h3> Select any one operation:
<input type="submit"  value="+" name="btnop">
<input type="submit"  value="-" name="btnop">
<input type="submit"  value="*" name="btnop">
<input type="submit"  value="/" name="btnop">
</h3><br>
</form>
</body>
</html>

calcservlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class calcservlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{

response.setContentType("text/html;charset=UTF-8");

PrintWriter out=response.getWriter();
int a=Integer.parseInt(request.getParameter("fnum"));
int b=Integer.parseInt(request.getParameter("snum")); 
String s1=request.getParameter("btnop"); 
int c=0; 
if (s1.equals("+")) { 
     c=a+b; 
     out.println("<h1>"+"Result="+c+"</h1>"); 
}
if (s1.equals("-")) {
     c=a-b;
     out.println("<h1>"+"Result="+c+"</h1>");
}
if (s1.equals("*")) {
     c=a*b; 
     out.println("<h1>"+"Result="+c+"</h1>");
}
if (s1.equals("/")) {
     c=a/b;
     out.println("<h1>"+"Result="+c+"</h1>");
}
}
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0">
    <servlet>
      <servlet-name>calcservlet</servlet-name>
      <servlet-class>calcservlet</servlet-class>
   </servlet>
    <servlet-mapping>
      <servlet-name>calcservlet</servlet-name>
      <url-pattern>/calc3</url-pattern>
   </servlet-mapping>
</web-app>

output:

client page output:


server page output:


No comments:

Post a Comment