-3
這應該是一個簡單的Jsp servlet計算器,但它並不工作。我可能是盲目的,但我想我錯過了一些東西。 有人可以幫我嗎?Java Jsp Servlet計算器
我有一個servlet:
package com.service.servlet;
import java.io.IOException
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Calculator")
public class Calculator extends HttpServlet {
private static final long serialVersionUID = 1L;
double result;
public Calculator() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String valueString = request.getParameter("value");
String resultString = request.getParameter("result");
double result = 0;
if (!resultString.isEmpty()) {
result = Double.parseDouble(resultString);
}
if (!valueString.isEmpty()) {
double value = Double.parseDouble(valueString);
Map parameters = request.getParameterMap();
if (parameters.containsKey("plus")) {
result += value;
} else if (parameters.containsKey("minus")) {
result -= value;
} else if (parameters.containsKey("divide")) {
result /= value;
} else if (parameters.containsKey("multiply")) {
result *= value;
}
request.setAttribute("value", result);
RequestDispatcher view = request.getRequestDispatcher("/Calc.jsp");
view.forward(request, response);
} else {
RequestDispatcher view = request.getRequestDispatcher("/Calc.jsp");
view.forward(request, response);
}
}
}
JSP文件是簡單和乾淨的價值和結果:
<form action="Calculator" method="POST">
<input name="value" type="text"/>
<input name="plus" type="submit" value="+" />
<input name="minus" type="submit" value="-" />
<input name="divide" type="submit" value="/" />
<input name="multiply" type="submit" value="*" />
<input name="result" readonly="readonly" type="text" value="" />";
</form>
您是否遇到錯誤? – Perdomoff
不,我沒有錯誤! – fiberhead
'request.setAttribute(「value」,result);'應該填充名爲'result'的輸入?此外,縮進是必須的可讀性 – 2015-11-02 19:05:26