2014-09-20 51 views
1

我編輯了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. 

回答

1

因爲你的ID輸入是空的,你去空字符串轉換爲int。這就是爲什麼你越來越:

java.lang.NumberFormatException: For input string: "" 

你想設置你的doGet方法要求的屬性項:

protected void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException { 

    RequestDispatcher view; 
    //not sure where are you getting your item from. DB?? 
    request.setAttribute("item", item); 
    view = request.getRequestDispatcher("/home/editexpense.jsp"); 
    view.forward(request,response); 
} 

更新 如果你想顯示項目的列表,你需要循環遍歷你的jsp中的列表。類似這樣的:

<table style="padding-left: 200px;margin-top: 10px;border: 1px"> 
    <tr> 
    <th>Expense title</th> 
    <th>Expense amount</th> 
    <th>Expense date</th> 
    <th>Action</th> 
    </tr> 
    <c:forEach items="${item}" var="it"> 
    //the form should be after the for loop 
    <form action="/edit" method="post"> 
    <tr> 
    <td><input name="itemname" type="text" required="required" value="it.id"/></td> 
    <td><input name="itemprice" type="text" required="required" value="it.itemPrice" />  </td> 
    <td><input name="transactiontime" type="date" id="datepicker" required="required" value="it.transactionTime"/></td> 
    <td> 
     <input type="submit" name="submit" value="update"/> 
     <input type="hidden" name="id" value="${it.id}"> 
    </td> 
    </tr> 
    //closing from tag 
    </form> 
    </c:forEach> 
</table> 
+0

@Sasvathan我照你所建議之上,但在編輯如下因素錯誤出現'org.apache.jasper.JasperException:發生異常處理JSP頁面/home/editexpense.jsp在32行 29: 30:<% - id的隱藏字段 - %> 31: 32: 33: 34:​​​​ 35:' – Ragataj 2014-09-21 16:17:30

+0

你可以在你的帖子上發佈完整的堆棧跟蹤嗎?你也可以發佈你的更新類嗎? – Sas 2014-09-21 17:37:33

+0

是的,我發佈了,現在可以看到上面的帖子@Sas – Ragataj 2014-09-21 18:02:58

0

將表達式語言(EL)${item.id}評估爲空字符串。這意味着物品的idnull

你可以做兩件事情:

  • 確保item存在於你想要的範圍(pageScoperequestScopesessionScope ...)。

  • 使用默認值。

    <input type="hidden" name="id" value="${not empty item.id ? item.id : '0' }"> 
    
+0

讓我試試@Paul Vargas,因爲你有建議。 – Ragataj 2014-09-22 13:55:31

+0

上面顯示的堆棧跟蹤錯誤@Paul Vargas – Ragataj 2014-09-22 14:01:28

+0

@ user3620849 Q:'dao.getItemFromdb(user)'返回列表中只有一個元素(item)?我在控制檯項目中看到的 – 2014-09-22 14:27:28