Monday, September 14, 2015

Math Operations in Servlet

I just started learning how to program in Java Servlet in this article I would like to share with you a program that I called Math Operations in Servlet. What does the program will do is to accept two number values and then the program will find the sum, difference, product and quotient of the two numbers given by the user.  The program is written in a simple manner that can be useful for anyone interested in web programming using servlet in Java.

If you  have some questions please send me an email at jake.r.pomperada@gmail.comand jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

form.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Math Operations in Servlet</title>
<style>
label{
    display: inline-block;
    float: left;
    clear: left;
    width: 185px;
    text-align: left;
}
input {
  display: inline-block;
  float: left;
}

h3 {
   font-family:arial;
   color:blue;
   }
</style>
</head>
<body bgcolor="lightgreen">
<form name='math' action='http://localhost:8080/addition/addition' method='get'>
 <div>
<font face="arial" size="3">
<h3> Math Operations in Servlet</h3> <br>
<label>Enter First Number </label> 
<input type='numberic' placeholder='first number' name='val1' size='10' autofocus required><br>
<label> Enter Second Number </label>
 <input type='numeric'  placeholder='second number' name='val2' size='10' required><br>
<br>
<input type='submit' name='submit' value='Compute' title='Click here to see the result.'>
<input type='reset'  name='reset' value='Clear' title='Click here to clear text box.'>
</font> </div>
</body>
</html>


addition_two_numbers.java

package addition;

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

public class addition_two_numbers extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException , ServletException
{
PrintWriter pr=res.getWriter();
res.setContentType("text/html");
try
  
 {
int x=Integer.parseInt(req.getParameter("val1"));
int y=Integer.parseInt(req.getParameter("val2"));

int total_sum = (x+y);
int total_product = (x*y);
int total_diff = (x-y);
int total_quotient = (x/y);

pr.println("<body bgcolor='lightgreen'>");
pr.println("<font face='arial' size='3'> ");
pr.println("<h3> <font color='Blue'> Display Computed Results </font> </h3>");
pr.println("<br>");
pr.println("The sum of  " + x + " and " + y 
 + " is "+ total_sum + ".<br>");
pr.println("The difference between  " + x + " and " + y 
 + " is "+ total_diff + ".<br>");
pr.println("The product of  " + x + " and " + y 
 + " is "+ total_product + ".<br>");
pr.println("The quotient of  " + x + " and " + y 
 + " is "+ total_quotient+ ".<br>");
pr.println("<br>");
pr.println("<h3><font color='Blue'>  End Program </font> </h3>");
pr.println("</font>");
}
catch(Exception e)
{
pr.println("Invalid Input value please try again");
}
} }



web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>addition</display-name>
    <servlet>
<servlet-name>addition</servlet-name>
<servlet-class>addition.addition_two_numbers</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addition</servlet-name>
<url-pattern>/addition</url-pattern>
</servlet-mapping>
</web-app>



No comments:

Post a Comment