VB icon

Math Operations in Servlet

Email
Submitted on: 9/14/2015 12:26:49 PM
By: Jake R. Pomperada  
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 4708
author picture
 
     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.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Math Operations in Servlet
// Description: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.
// By: Jake R. Pomperada
//**************************************

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>


Other 53 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.