抓取在jsp中通过get方法传递的int

时间:2017-03-13 06:06:04

标签: java jsp

我已经设置了一个删除按钮来删除db中的一行。它使用get方法将id作为参数。

<a class="btn btn-sm btn-danger" href="teacherDelete.jsp?=<%=t.getId()%>">Delete</a>

这是我的代码teacherDelete.jsp,用于抓取id并删除一行:

        <% 

        if (request.getMethod().equals("GET")) {
                TeacherDAO tdao = new TeacherDAOImpl();
                Teacher t = new Teacher();
                t.setId(Integer.parseInt(request.getParameter("id")));                            
                tdao.delete(t);
           // New location to be redirected
           String site = new String("teachersShow.jsp");
           response.setStatus(response.SC_MOVED_TEMPORARILY);
           response.setHeader("Location", site);                     
            }
    %>

org.apache.jasper.JasperException: java.lang.NumberFormatException: null我收到HTTP Status 500错误。

如何获取传入的参数并将其传递给我的方法?

2 个答案:

答案 0 :(得分:1)

在转换为int之前,应该检查它是否包含null

<a class="btn btn-sm btn-danger" href="teacherDelete.jsp?=<%=t.getId()%>">Delete</a>

所以应该

<a class="btn btn-sm btn-danger" href="teacherDelete.jsp?id=<%=t.getId()%>">Delete</a>

答案 1 :(得分:1)

您错过了代码中的参数ID teacherDelete.jsp?=

  <a class="btn btn-sm btn-danger" href="teacherDelete.jsp?id=<%=t.getId()%>">Delete</a>