Showing posts with label sum of numbers in jsp. Show all posts
Showing posts with label sum of numbers in jsp. Show all posts

Thursday, December 25, 2014

Addition of Five Numbers in Java Server Pages


One of the interesting web scripting programming language that is based on Java is Java Server Pages or JSP this programming language is based on Java but their are some similarities in terms of syntax in PHP. As I having my christmas vacation at home I try to keep myself busy by learning other programming language one of its is JSP or Java Server Pages. At first I find it very confusing and hard just to write a single line of code primary because majority of my experience in based in using PHP as my primary web programming language.

Basically before you can write your first JSP program you must download and install the Apache Tomcat Web Server that enables you to run your JSP script on it. The tomcat installer is very small and very easy to install and configure it is very similar to Apache web server in many ways the only difference it can only runs Java related codes like JSP and Servlets. 

What are program will do is quite simple it will ask the user to enter five numbers in our form and then send those five number values to another page using POST method and then our JSP script will compute the sum of the five numbers. The code is very simple and easy to understand once you have a basic background in Java programming you can easy leverage your existing knowledge in JSP programming.

I hope you will find my work useful and beneficial I'm just a beginner in terms of programming using JSP. If you have some questions please send me an email at jakerpomperda@gmail.com and jakerpomperada@yahoo.com.

People here in the Philippines who wish to contact me can reach me through my mobile numbers at 09173083260 and 09993969756.

Thank you very much and Happy Programming




Sample Output of Our Program

Program Listing

index.htm

<html>
    <head>
        <title>Addition of Five Numbers</title>
    </head>
    
     <body bgcolor="lightgreen">
 <br><br>
<h2> Addition of Five Numbers </h2>
<br>
        <form action="./add.jsp">
            First number: <input type="text" name="val1"/><br>
            Second number: <input type="text" name="val2"/><br>
            Third number: <input type="text" name="val3"/><br>
            Fourth number: <input type="text" name="val4"/><br>
            Fifth number: <input type="text" name="val5"/><br>
          <br><br>
 <input type="submit" value="Solve" title="Click here to find the sum." />
        </form>
    </body>
</html>

add.jsp

<html>
    <head>
        <title>Addition of Five Numbers</title>
    </head>
   
<body bgcolor="lightgreen">
 <br><br>
<%
int a,b,c,d,e1,sum;
try{
a=Integer.parseInt(request.getParameter("val1"));
b=Integer.parseInt(request.getParameter("val2"));
c=Integer.parseInt(request.getParameter("val3"));
d=Integer.parseInt(request.getParameter("val4"));
e1=Integer.parseInt(request.getParameter("val5"));

sum=(a+b+c+d+e1);
out.println("<h1> The total sum of " +a+ ", " +b+" ,"+c+" , "+d+", "+e1 + " is " + sum + ".</h1>");
}
catch(Exception e)
{
out.println("<script>alert('You entered invalid number',); </script>");
}
%>
    </body>
</html>