你好我只是想知道为什么我在尝试运行这个servlet时会得到一个NumberFormatException ...我已经尝试了很多不同的东西,现在正在寻找一些帮助。感谢。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String artistName = request.getParameter("artistName");
String songTitle = request.getParameter("songTitle");
String albumTitle = request.getParameter("albumTitle");
String yearReleased = request.getParameter("yearReleased");
String weekAtNumberOne = request.getParameter("weekAtNumberOne");
int yearReleasedInt = Integer.parseInt(yearReleased);
int weekAtNumberOneInt = Integer.parseInt(weekAtNumberOne);
ServletContext sc = this.getServletContext();
String dbDriver = sc.getInitParameter("driver");
String dbURL = sc.getInitParameter("url");
String dbName = sc.getInitParameter("database");
String dbUser = sc.getInitParameter("dbusername");
String dbPassword = sc.getInitParameter("dbpassword");
DBConnector.createConnection(dbDriver, dbURL, dbName, dbUser, dbPassword);
Song song = new Song(artistName, songTitle, weekAtNumberOneInt , yearReleasedInt, albumTitle);
SongDAO songDAO = new SongDAO();
if (songDAO.ArtistCheck(song) == true)
{
RequestDispatcher rd = request.getRequestDispatcher("/addNewArtist.jsp");
rd.forward(request, response);
}
else
{
songDAO.AddNewSong(song);
RequestDispatcher rd = request.getRequestDispatcher("GetAllSongs.do");
rd.forward(request, response);
}
}
这是Tomcat日志:
Mar 02, 2014 12:28:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AddNewSingleServlet] in context with path [/test] threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at com.assignment2.Servlets.AddNewSingleServlet.doPost(AddNewSingleServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
String dbName = sc.getInitParameter("database"); is line 67 in the error.
答案 0 :(得分:1)
您正在尝试将空字符串解析为无效的数字
java.lang.NumberFormatException: For input string: ""
您可能希望在请求数据上添加服务器端验证器
答案 1 :(得分:0)
你得到这个异常是因为你试图解析一个空字符串,我怀疑这个参数:
String yearReleased = request.getParameter("yearReleased");
String weekAtNumberOne = request.getParameter("weekAtNumberOne");
确保请求参数不为空,然后尝试解析它们
int yearReleasedInt = Integer.parseInt(yearReleased);
if(yearReleasedInt != null || !yearReleasedInt.equals("")) {
int yearReleasedInt = Integer.parseInt(yearReleased);
}
或者您可以在不对params进行验证的情况下捕获异常,但这取决于您的业务案例。
int yearReleasedInt = 0;
try {
yearReleasedInt = Integer.parseInt(yearReleased);
} catch(NumberFormatException ex) {
//handle exception
}
答案 2 :(得分:0)
以下任何一种:
int yearReleasedInt = Integer.parseInt(yearReleased);
int weekAtNumberOneInt = Integer.parseInt(weekAtNumberOne);
或两者都可能导致错误。尝试打印yearReleased
和weekAtNumberOne
并查看String
值。
您可以执行以下操作:
int yearReleasedInt = -1;
try {
yearReleasedInt = Integer.parseInt(yearReleased);
} catch(Exception ex){
//handle error
}
if(yearReleasedInt != -1) {
// do your work.
}