这是一个简单的计算器,带有2个操作:'+'& ' - '。 当我不久前运行它时,一切正常。但在某些时候我犯了错误,无法理解发生了什么。
这是我的来源:
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<form action="./" method="get">
<label>First number:</label> <input type="text" name="num1" /> <br />
<label>Second number: </label> <input type="text" name="num2" /> <br />
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input type="submit" value="submit" />
</form>
</body>
</html>
XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Calculator</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Calculator</servlet-name>
<servlet-class>ua.nure.nechyporuk.Calculator</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Calculator</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
的.java
package ua.nure.nechyporuk;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Calculator extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
int a1 = Integer.parseInt(request.getParameter("num1"));
int a2 = Integer.parseInt(request.getParameter("num2"));
String operator = request.getParameter("operator");
if (operator.equals("+")&& operator != null) {
request.setAttribute("operation", "addition");
request.setAttribute("res", a1+a2);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
if (operator.equals("-")&& operator != null) {
request.setAttribute("operation", "subtract");
request.setAttribute("res", a1-a2);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Result of calculation:</title>
</head>
<body>
<h1>Result of ${operation}</h1>
<h1>${res}</h1>
<a href= "http://localhost:8081/Calculator">back to calculator</a>;
</body>
</html>
我无法理解为什么会出现此错误。 像html页面没时间工作。 Tomkat我设置为8081端口。
例外:
java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at ua.nure.nechyporuk.Calculator.doGet(Calculator.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
而不是使用此
Integer.parseInt(request.getParameter("num1"));
试试这个。只需在"" +
request.getParameter("num1")
即可
Integer.parseInt("" + request.getParameter("num1"));
希望这可以解决您的问题。并确保在字段中输入有效的整数
<强>更新强>
检查null
值
String str=request.getParameter("num1");
int a=0;
if(str!=null){
a = Integer.parseInt("" + str);
}