2016-03-06 64 views
1

我正試圖在我的項目中實現CRUD功能。我能夠在jsp上顯示數據庫表,但是當我嘗試刪除一個條目時,出現這個錯誤:(我發現servlet中有一行存在問題,但不明白爲什麼它不能夠檢索bookingID):request.getParameter正在檢索'null'

Warning: StandardWrapperValve[UserController]: Servlet.service() for servlet UserController threw exception 
 
java.lang.NumberFormatException: null 
 
\t at java.lang.Integer.parseInt(Integer.java:542) 
 
\t at java.lang.Integer.parseInt(Integer.java:615) 
 
\t at com.daniel.controller.UserController.doGet(UserController.java:34) 
 
\t at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
 
\t at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
 
\t at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
 
\t at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
 
\t at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
 
\t at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
 
\t at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
 
\t at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
 
\t at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
 
\t at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) 
 
\t at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283) 
 
\t at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) 
 
\t at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 
 
\t at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) 
 
\t at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) 
 
\t at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 
 
\t at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
 
\t at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) 
 
\t at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) 
 
\t at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) 
 
\t at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) 
 
\t at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
 
\t at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) 
 
\t at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 
 
\t at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 
 
\t at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) 
 
\t at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 
 
\t at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591) 
 
\t at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) 
 
\t at java.lang.Thread.run(Thread.java:745)

Booking.java

package com.daniel.model; 
 

 
public class Booking { 
 
\t 
 
\t private int bookingID = -1; 
 
\t private String bookingDate = null; 
 
\t private String reasoning = null; 
 
\t private String bookingStatus = null; 
 
\t private String bookingReview = null; 
 
\t 
 
\t public int getBookingID() { 
 
\t \t return bookingID; 
 
\t } 
 
\t public void setBookingID(int bookingID) { 
 
\t \t this.bookingID = bookingID; 
 
\t } 
 
\t public String getBookingDate() { 
 
\t \t return bookingDate; 
 
\t } 
 
\t public void setBookingDate(String bookingDate) { 
 
\t \t this.bookingDate = bookingDate; 
 
\t } 
 
\t public String getReasoning() { 
 
\t \t return reasoning; 
 
\t } 
 
\t public void setReasoning(String reasoning) { 
 
\t \t this.reasoning = reasoning; 
 
\t } 
 
\t public String getBookingStatus() { 
 
\t \t return bookingStatus; 
 
\t } 
 
\t public void setBookingStatus(String bookingStatus) { 
 
\t \t this.bookingStatus = bookingStatus; 
 
\t } 
 
\t public String getBookingReview() { 
 
\t \t return bookingReview; 
 
\t } 
 
\t public void setBookingReview(String bookingReview) { 
 
\t \t this.bookingReview = bookingReview; 
 
\t } 
 
\t @Override 
 
\t public String toString() { 
 
\t \t return "Booking [bookingID=" + bookingID + ", bookingDate=" + bookingDate 
 
\t     + ", reasoning=" + reasoning + ", bookingStatus=" + bookingStatus + ", bookingReview=" 
 
\t     + bookingReview + "]"; 
 

 
\t } 
 

 

 
\t 
 
}

BookingDao.java

package com.daniel.dao; 
 

 
import java.sql.Connection; 
 
import java.sql.PreparedStatement; 
 
import java.sql.ResultSet; 
 
import java.sql.SQLException; 
 
import java.sql.Statement; 
 
import java.util.ArrayList; 
 
import java.util.List; 
 

 
import com.daniel.model.Booking; 
 
import com.daniel.util.DbUtil; 
 

 

 
public class BookingDao { 
 
\t 
 
\t private Connection connection; 
 

 
    public BookingDao() { 
 
     connection = DbUtil.getConnection(); 
 
    } 
 

 
    public void addUser(Booking booking) { 
 
     try { 
 
      PreparedStatement preparedStatement = connection 
 
        .prepareStatement("insert into booking(booking_id,booking_date,reasoning,booking_status) values (?, ?, ?, 'Upcoming')"); 
 
      // Parameters start with 1 
 
      preparedStatement.setInt(1, booking.getBookingID()); 
 
      preparedStatement.setString(2, booking.getBookingDate()); 
 
      preparedStatement.setString(3, booking.getReasoning()); 
 
      preparedStatement.executeUpdate(); 
 

 
     } catch (SQLException e) { 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 

 
    public void deleteUser(int bookingID) { 
 
     try { 
 
      PreparedStatement preparedStatement = connection 
 
        .prepareStatement("delete from booking where booking_id=?"); 
 
      // Parameters start with 1 
 
      preparedStatement.setInt(1, bookingID); 
 
      preparedStatement.executeUpdate(); 
 

 
     } catch (SQLException e) { 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 

 
    public void updateUser(Booking booking) { 
 
     try { 
 
      PreparedStatement preparedStatement = connection 
 
        .prepareStatement("update booking set booking_date=?, reasoning=?" + 
 
          "where booking_id=?"); 
 
      // Parameters start with 1 
 
      preparedStatement.setString(1, booking.getBookingDate()); 
 
      preparedStatement.setString(2, booking.getReasoning()); 
 
      preparedStatement.setInt(3, booking.getBookingID()); 
 
      preparedStatement.executeUpdate(); 
 

 
     } catch (SQLException e) { 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 

 
    public List<Booking> getAllUsers() { 
 
     List<Booking> bookings = new ArrayList<Booking>(); 
 
     try { 
 
      Statement statement = connection.createStatement(); 
 
      ResultSet rs = statement.executeQuery("select * from booking"); 
 
      while (rs.next()) { 
 
       Booking booking = new Booking(); 
 
       booking.setBookingID(rs.getInt(1)); 
 
       booking.setBookingDate(rs.getString(2)); 
 
       booking.setReasoning(rs.getString(3)); 
 
       booking.setBookingStatus(rs.getString(4)); 
 
       booking.setBookingReview(rs.getString(5)); 
 
       bookings.add(booking); 
 
       System.out.println(booking); 
 
      } 
 
     } catch (SQLException e) { 
 
      e.printStackTrace(); 
 
     } 
 

 
     return bookings; 
 
    } 
 

 
    public Booking getUserById(int bookingID) { 
 
     Booking booking = new Booking(); 
 
     try { 
 
      PreparedStatement preparedStatement = connection. 
 
        prepareStatement("select * from booking where booking_id=?"); 
 
      preparedStatement.setInt(1, bookingID); 
 
      ResultSet rs = preparedStatement.executeQuery(); 
 

 
      if (rs.next()) { 
 
      \t booking.setBookingID(rs.getInt(1)); 
 
       booking.setBookingDate(rs.getString(2)); 
 
       booking.setReasoning(rs.getString(3)); 
 
       booking.setBookingStatus(rs.getString(4)); 
 
       booking.setBookingReview(rs.getString(5)); 
 
      } 
 
     } catch (SQLException e) { 
 
      e.printStackTrace(); 
 
     } 
 

 
     return booking; 
 
    } 
 

 

 
}

UserController.java

package com.daniel.controller; 
 

 

 
import java.io.IOException; 
 

 

 
import javax.servlet.RequestDispatcher; 
 
import javax.servlet.ServletException; 
 
import javax.servlet.http.HttpServlet; 
 
import javax.servlet.http.HttpServletRequest; 
 
import javax.servlet.http.HttpServletResponse; 
 

 
import com.daniel.dao.BookingDao; 
 
import com.daniel.model.Booking; 
 

 
public class UserController extends HttpServlet { 
 
\t 
 
\t private static final long serialVersionUID = 1L; 
 
\t  private static String INSERT_OR_EDIT = "/user.jsp"; 
 
\t  private static String LIST_USER = "/listUser.jsp"; 
 
\t  private BookingDao dao; 
 

 
\t  public UserController() { 
 
\t   super(); 
 
\t   dao = new BookingDao(); 
 
\t  } 
 

 
\t  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t   String forward=""; 
 
\t   String action = request.getParameter("action"); 
 

 
\t   if (action.equalsIgnoreCase("delete")){ 
 
\t   \t System.out.println(request.getParameter("bookingID")); 
 
\t    int bookingID = Integer.parseInt(request.getParameter("bookingID")); 
 
\t    dao.deleteUser(bookingID); 
 
\t    forward = LIST_USER; 
 
\t    request.setAttribute("users", dao.getAllUsers());  
 
\t   } else if (action.equalsIgnoreCase("edit")){ 
 
\t    forward = INSERT_OR_EDIT; 
 
\t    int bookingID = Integer.parseInt(request.getParameter("bookingID")); 
 
\t    Booking booking = dao.getUserById(bookingID); 
 
\t    request.setAttribute("booking", booking); 
 
\t   } else if (action.equalsIgnoreCase("listUser")){ 
 
\t    forward = LIST_USER; 
 
\t    request.setAttribute("users", dao.getAllUsers()); 
 
\t   } else { 
 
\t    forward = INSERT_OR_EDIT; 
 
\t   } 
 

 
\t   RequestDispatcher view = request.getRequestDispatcher(forward); 
 
\t   view.forward(request, response); 
 
\t  } 
 

 
\t  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t   Booking booking = new Booking(); 
 
\t   booking.setBookingDate(request.getParameter("bookingDate")); 
 
\t   booking.setReasoning(request.getParameter("reasoning")); 
 
\t   String bookingID = request.getParameter("bookingID"); 
 
\t   if(bookingID == null || bookingID.isEmpty()) 
 
\t   { 
 
\t    dao.addUser(booking); 
 
\t   } 
 
\t   else 
 
\t   { 
 
\t   booking.setBookingID(Integer.parseInt(bookingID)); 
 
\t    dao.updateUser(booking); 
 
\t   } 
 
\t   RequestDispatcher view = request.getRequestDispatcher(LIST_USER); 
 
\t   request.setAttribute("users", dao.getAllUsers()); 
 
\t   view.forward(request, response); 
 
\t  } 
 

 

 
}

listUser.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> 
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 
 
<!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=EUC-KR"> 
 
<title>Show All Users</title> 
 
</head> 
 
<body> 
 
    <table border=1> 
 
     <thead> 
 
      <tr> 
 
       <th>User Id</th> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>DOB</th> 
 
       <th>Email</th> 
 
       <th colspan=2>Action</th> 
 
      </tr> 
 
     </thead> 
 
     <tbody> 
 
      <c:forEach items="${users}" var="booking"> 
 
       <tr> 
 
        <td><c:out value="${booking.bookingID}" /></td> 
 
        <td><c:out value="${booking.bookingDate}" /></td> 
 
        <td><c:out value="${booking.reasoning}" /></td> 
 
        <td><c:out value="${booking.bookingStatus}" /></td> 
 
        <td><c:out value="${booking.bookingReview}" /></td> 
 
        <td><a href="UserController?action=edit&userId=<c:out value="${booking.bookingID}"/>">Update</a></td> 
 
        <td><a href="UserController?action=delete&userId=<c:out value="${booking.bookingID}"/>">Delete</a></td> 
 
       </tr> 
 
      </c:forEach> 
 
     </tbody> 
 
    </table> 
 
    <p><a href="UserController?action=insert">Add User</a></p> 
 
</body> 
 
</html>

回答

0

在模板中設置的鏈接與參數userId=<c:out value="${booking.bookingID}"/>,然後嘗試回讀參數與request.getParameter("bookingID")

由於參數名爲userId而不是bookingID對getParameter的調用返回null,因此該名稱的參數不存在。

這可以通過確保您在兩個地方使用相同的名稱來解決。

3

您的鏈接刪除使用以下網址:

UserController?action=delete&userId=<c:out value="${booking.bookingID}"/> 

而且你的代碼使用

request.getParameter("bookingID") 

所以,很顯然,該bookingID參數爲空,因爲在URL中有這樣的參數。該參數被命名爲userId

請注意,使用鏈接刪除的東西是一個非常非常糟糕的主意。任何人都可以向您發送包含刪除網址的鏈接,如果您點擊該鏈接:Boom!你已經刪除了一個用戶。

更糟的是:如果該頁面被公開,GoogleBot(和所有其他搜索引擎機器人)將點擊頁面中的每個鏈接來抓取您的網站,從而刪除每個用戶。

使用與POST一起提交的表單。

+0

工作,謝謝。 – abcdefg

+1

@PragnaniKinnera這裏投票是無記名投票。你沒有證據表明投票的方式,也沒有任何商業投機。 – EJP