我編輯了servlet。解析錯誤只是編輯時的問題。雖然我一直在努力,因爲一個月的一半,我不能夠solve.Please幫我Servlet中Id解析錯誤
package com.intermediateDemo.home.controller;
import com.intermediateDemo.home.dao.ItemDao;
import com.intermediateDemo.home.dao.ItemDaoFactory;
import com.intermediateDemo.home.dto.ItemBean;
import com.intermediateDemo.login.dto.LoginBean;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class EditExpense extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, ServletException, IOException {
try {
ItemBean item = new ItemBean();
ItemDao dao = ItemDaoFactory.getItemDao();
HttpSession session = request.getSession(false);
LoginBean user = (LoginBean) session.getAttribute("user");
item.setId(Integer.parseInt(request.getParameter("id")));
item.setItemName(request.getParameter("itemname"));
item.setItemPrice(Double.valueOf(request.getParameter("itemprice")));
item.setTransactionTime(request.getParameter("transactiontime"));
item.setEmail(user.getEmail());
System.out.println("at edit servlet" +item);
dao.updateexpense(item, user);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
response.sendRedirect("homeservlet");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view;
view = request.getRequestDispatcher("/home/editexpense.jsp");
view.forward(request,response);
}
}
和jsp採取inpute是
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>editing</title>
</head>
<body>
<jsp:include page="../includes/header.jsp"/>
<div id="content" style="margin-left: 330px;margin-bottom: 10px">
<form action="/edit" method="post">
<table style="padding-left: 200px;margin-top: 10px;border: 1px">
<tr><td>Expense title</td><td style="background-color: limegreen">
<input name="itemname" type="text" required="required"/></td></tr>
<tr><td>Expense amount</td><td style="background-color: limegreen">
<input name="itemprice" type="text" required="required"/> </td></tr>
<tr><td>Expense date</td><td style="background-color: limegreen;border-bottom-color: limegreen">
<input name="transactiontime" type="date" id="datepicker" required="required"/></td></tr>
<%--Hidden field for id--%>
<input type="hidden" name="id" value="${item.id}">
<tr><td> </td><td><input type="submit" name="submit" value="update"/></td></tr>
</table>
</form>
</div>
<jsp:include page="../includes/footer.jsp"/>
</body>
</html>
和錯誤在控制檯編輯後發現下面,而不是任何change.But同時刪除刪除servlet有沒有id的整數解析錯誤,以同樣的方式
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.intermediateDemo.home.controller.EditExpense.doPost(EditExpense.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
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)
我更新的類是
package com.intermediateDemo.home.controller;
import com.intermediateDemo.home.dao.ItemDao;
import com.intermediateDemo.home.dao.ItemDaoFactory;
import com.intermediateDemo.home.dto.ItemBean;
import com.intermediateDemo.login.dto.LoginBean;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
public class EditExpense extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, ServletException, IOException {
try {
ItemBean item = new ItemBean();
ItemDao dao = ItemDaoFactory.getItemDao();
HttpSession session = request.getSession(false);
LoginBean user = (LoginBean) session.getAttribute("user");
item.setId(Integer.parseInt(request.getParameter("id")));
item.setItemName(request.getParameter("itemname"));
item.setItemPrice(Double.valueOf(request.getParameter("itemprice")));
item.setTransactionTime(request.getParameter("transactiontime"));
item.setEmail(user.getEmail());
System.out.println("at edit servlet" +item);
dao.updateexpense(item, user);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
response.sendRedirect("homeservlet");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view;
try {
HttpSession session = request.getSession(false);
LoginBean user = (LoginBean) session.getAttribute("user");
ItemDao dao = ItemDaoFactory.getItemDao();
List<ItemBean> item = dao.getItemFromdb(user);
request.setAttribute("item", item);
System.out.println("@ edit servlet ");
for(ItemBean item1:item){
System.out.println(item1.getId());
System.out.println(item1.getTransactionTime());
System.out.println(item1.getItemPrice());
System.out.println(item1.getItemName());
}
} catch (SQLException e) {
e.printStackTrace();
}
view = request.getRequestDispatcher("/home/editexpense.jsp");
view.forward(request,response);
}
}
堆棧跟蹤誤差爲
type Exception report
message
description The server encountered an internal error() that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /home/editexpense.jsp at line 30
27: <input name="transactiontime" type="date" id="datepicker" required="required" /></td></tr>
28:
29: <%--Hidden field for id--%>
30: <input type="hidden" name="id" value="${item.id}">
31:
32:
33: <tr><td> </td><td><input type="submit" name="submit" value="update"/> </td></tr>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.intermediateDemo.home.controller.EditExpense.doGet(EditExpense.java:65)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NumberFormatException: For input string: "id"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ListELResolver.coerce(ListELResolver.java:174)
javax.el.ListELResolver.getValue(ListELResolver.java:52)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
org.apache.jsp.home.editexpense_jsp._jspService(editexpense_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.intermediateDemo.home.controller.EditExpense.doGet(EditExpense.java:65)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.
@Sasvathan我照你所建議之上,但在編輯如下因素錯誤出現'org.apache.jasper.JasperException:發生異常處理JSP頁面/home/editexpense.jsp在32行 29: 30:<% - id的隱藏字段 - %> 31: 32: 33: 34:
你可以在你的帖子上發佈完整的堆棧跟蹤嗎?你也可以發佈你的更新類嗎? – Sas 2014-09-21 17:37:33
是的,我發佈了,現在可以看到上面的帖子@Sas – Ragataj 2014-09-21 18:02:58