使用 EL 将 JSP 数据传输到 Servlet

发布于 2024-10-20 17:17:24 字数 1247 浏览 1 评论 0原文

再会!

我正在尝试删除一条记录。但在删除它之前,我将使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-10-27 17:17:24

您需要将其作为相同形式的隐藏输入值传递给下一个请求。

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

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}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文