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 pointer 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.