开发者

JSP Data to Servlet using EL

开发者 https://www.devze.com 2023-02-15 03:30 出处:网络
Good day! I am trying to delete a record. But before I delete it, i will show a confirmation message using JSP as follows: (ID and name from \"Select ID\" Servlet)

Good day!

I am trying to delete a record. But before I delete it, i will show a confirmation message using JSP as follows: (ID and name from "Select ID" Servlet)

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

And my delete code is as follows:

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);
        }
}

But I am having a null poin开发者_运维百科ter exeption. It seems like the ID cannot be read. (But I am not sure). How can I pass data from JSP to Servlet using EL? What causes the error and how can I resolve it? Thank you.


You need to pass it to the next request as a hidden input value in the same form.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号