使用 EL 将 JSP 数据传输到 Servlet
再会!
我正在尝试删除一条记录。但在删除它之前,我将使用 JSP 显示一条确认消息,如下所示:(“Select 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);
}
}
但我遇到了 空指针异常
。看来ID无法读取。 (但我不确定)。如何使用 EL 将数据从 JSP 传递到 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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将其作为相同形式的隐藏输入值传递给下一个请求。
You need to pass it to the next request as a hidden input value in the same form.