gpt4 book ai didi

java - JSP 项目 - 没有从输入标记中获取双值

转载 作者:行者123 更新时间:2023-12-01 12:55:40 25 4
gpt4 key购买 nike

我正在使用 Eclipse 开发 JSP 项目。我尝试根据在 JSP 中创建的方法/函数进行一些计算,但按下按钮时,它始终显示 Sum、Sub 和 Mul 的初始化值 0.0,而不是从输入标记获取输入,然后执行计算。以下是我当前的代码:

<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>

请帮忙。谢谢。

最佳答案

您的输入元素不在 <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>

关于java - JSP 项目 - 没有从输入标记中获取双值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com