2011-03-07 47 views
1

美好的一天!使用EL將JSP數據轉換爲Servlet

我想刪除一條記錄。但在此之前我刪除它,我將展示使用JSP的確認消息如下:(摘自「ID」 Servlet的ID和姓名)

<form action = "Delete">  
    Id: ${student.id} <br> 
    <input type="submit" value="Delete"> 
    </form> 

而且我刪除代碼如下:

public class Delete extends HttpServlet { 

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

     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     String query = "Delete FROM customer WHERE id = ?"; 

     try { 

      Context context = new InitialContext(); 
      DataSource ds = (DataSource) context.lookup("jdbc/myDatasource"); 
      Connection con = ds.getConnection(); 
      PreparedStatement stmt = con.prepareStatement(query); 
      int id = Integer.parseInt(request.getParameter("id")); //ERROR HERE? 
      stmt.setInt(1, id); 
      stmt.executeUpdate(query); 

     } catch (Exception ex) { 
      out.println("Error.... " + ex); 
     } 
} 

但我有一個null pointer exeption。看起來這個ID不能被讀取。 (但我不確定)。如何使用EL將數據從JSP傳遞給Servlet?導致錯誤的原因是什麼?如何解決?謝謝。

回答

3

您需要將它作爲隱藏的輸入值傳遞給下一個請求。

<input type="hidden" name="id" value="${student.id}" /> 
+0

啊好的:)你在回答這麼快。謝謝。 – newbie 2011-03-07 17:31:45