我正在使用Eclipse工作JSP项目。我尝试从我在JSP中创建的方法/函数进行一些计算,但是它仍然显示Sum,Sub和&的初始化值为0.0。当按下按钮而不是从输入标签获取输入然后进行计算时,Mul。以下是我目前的代码:
<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>
请帮忙。谢谢。
答案 0 :(得分:1)
您的输入元素不在<form>
标记内!
如何在没有表单的情况下提交页面。您是否在问题中提供了确切的HTML?
应该是,
<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>
答案 1 :(得分:0)
将所有输入参数放在表单中,然后按下按钮执行操作:
<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>