7d0
7d0

Reputation: 79

JSP project - no double values from input tags taken

I am working a JSP project using Eclipse. I tried to make some calculations from the methods/functions that I created in JSP, but it keeps showing the initialization values of 0.0 for Sum, Sub, & Mul when the buttons are pressed instead of taking the input from input tags and then doing the calculations. Below is my current code:

<pre><code>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%!     
public double add(double a, double b) {

    return a + b;
}

public double subtract(double a, double b) {

    return a - b;
}

public double multiply(double a, double b) {

    return a * b;
}   
%>

<%
    PrintWriter Out = response.getWriter();

    double Sum = 0.0, Sub = 0.0, Mul = 0.0;

    if (request.getParameter("stNum") != null  && request.getParameter("ndNum") !=     null) {

        Sum = add(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
        Sub = subtract(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
        Mul = multiply(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
    }

    //Sum = add(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
    //Sub = subtract(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
    //Mul = multiply(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Mathematics Calculations !!!</title>
</head>
<body>
    <label>Enter the first number: </label><input type="text" id="stNum" name="stNum" width="30" />

    <br />
    <br />

    <label>Enter the second number: </label><input type="text" id="ndNum" name="ndNum" width="30" />

    <br />
    <br />

    <input type="button" value="Sum" onclick="alert('The sum is: <%=Sum %>');" />

    <br />
    <br />

    <input type="button" value="Subtract" onclick="alert('The subtraction is: <%=Sub %>');" />

    <br />
    <br />

    <input type="button" value="Multiplication" onclick="alert('The multiplication is: <%=Mul %>');" />
</body>
</html>
</code></pre>

Please help. Thank you.

Upvotes: 0

Views: 1616

Answers (2)

Java Enthusiast
Java Enthusiast

Reputation: 664

Put all the input parameters inside the form and perform the operation on button click as :

<form method="POST" action="page_url">
   <label>Enter the first number: </label><input type="text" id="stNum" name="stNum" width="30" />

   <br />
   <br />
   <label>Enter the second number: </label><input type="text" id="ndNum" name="ndNum" width="30" />

   <br />
   <br />

   <input type="button" value="Sum" name="sum" />

   <br />
   <br />

   <input type="button" value="Subtract" nam="dif" />

   <br />
   <br />

   <input type="button" value="Multiplication" name="mul" />
   <%
   if(request.getParameter("sum")!=null)
   {
        Sum = add(Double.parseDouble(request.getParameter("stNum")), Double.parseDouble(request.getParameter("ndNum")));

   }%>
       <script>
           alert("The sum is "+Sum);
       </script>
   <%
   .....//Similarly for subtraction and multiplication
    %>
</form>

Upvotes: 0

jatanp
jatanp

Reputation: 4102

Your input elements are not inside <form> tag !

How the page is getting submitted without form. Have you provided the exact HTML in the question ?

It should be,

<body>
<form method="POST" action="page_url">
    <label>Enter the first number: </label><input type="text" id="stNum" name="stNum" width="30" />

    <br />
    <br />

    <label>Enter the second number: </label><input type="text" id="ndNum" name="ndNum" width="30" />

    <br />
    <br />

    <input type="button" value="Sum" onclick="alert('The sum is: <%=Sum %>');" />

    <br />
    <br />

    <input type="button" value="Subtract" onclick="alert('The subtraction is: <%=Sub %>');" />

    <br />
    <br />

    <input type="button" value="Multiplication" onclick="alert('The multiplication is: <%=Mul %>');" />
</form>
</body>

Upvotes: 1

Related Questions