美好的一天!
我正在尝试删除记录。但在删除它之前,我将使用JSP显示一条确认消息,如下所示:( ID和来自“Select ID”Servlet的名称)
<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?导致错误的原因是什么?如何解决?谢谢。
答案 0 :(得分:3)
您需要将其作为隐藏输入值以相同的形式传递给下一个请求。
<input type="hidden" name="id" value="${student.id}" />