Showing posts with label addition of five numbers in Java Servlet. Show all posts
Showing posts with label addition of five numbers in Java Servlet. Show all posts

Saturday, July 25, 2015

Addition of Five Numbers Using Java Servlet

Learning to program in Java is a journey not a destination.  This program will allow the user to enter five numbers in a form and then it will display the total sum of five numbers using Java Servlet.

If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.







Sample Program Output


Program Listing

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Addition of Five Numbers</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
       body {
           background:lightgreen; 
           font-family: arial;
        }
        h2 {
            font-family: arial;
        }  
    </style>  </head>  
    <body>
  <h2> Addition of Five Numbers in Java Servlet </h2>
<form name='addition' action='http://localhost:8080/WebApplication1/addition' method='get'>
Enter First  Value <input type='text' name='value1'><br>
Enter Second Value <input type='text' name='value2'><br>
Enter Third  Value <input type='text' name='value3'><br>
Enter Fourth Value <input type='text' name='value4'><br>
Enter Fifth Value <input type='text' name='value5'><br><br>
<input type='submit' name='submit' value='Add Values' title='Click here to find the sum.'>
</form>
</body>
</html>


additional.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author jake.r.pomperada
 */
@WebServlet(urlPatterns = {"/addition"})
public class addition extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException , ServletException
{
PrintWriter pr=res.getWriter();
res.setContentType("text/html");
try
{
int a=Integer.parseInt(req.getParameter("value1"));
int b=Integer.parseInt(req.getParameter("value2"));
int c=Integer.parseInt(req.getParameter("value3"));
int d=Integer.parseInt(req.getParameter("value4"));
int e=Integer.parseInt(req.getParameter("value5"));

int total_sum=(a+b+c+d+e);
pr.println("<body bgcolor='lightgreen'>");    
pr.println("<h1> THE RESULT </h1>");
pr.println("<br>");
pr.println("<h2> The sum of " +a + "," 
          + b + ", " + c + ", " + d + ", "
          + e + " is "+ total_sum +". </h2");
pr.println("</body>");

}
catch(Exception e)
{
pr.println("<body bgcolor='lightgreen'>");    
pr.println("<font face='aria'>Invalid Input Value Try Again");
pr.println("</font> </body>");
}
} }

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>addition</servlet-name>
        <servlet-class>addition</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>addition</servlet-name>
        <url-pattern>/addition</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file> index.html</welcome-file>
     </welcome-file-list>   
</web-app>