Integer.parseInt()没有解析String" id"来自servlet url参数

时间:2018-01-16 12:02:10

标签: java jsp servlets

我有一个.jsp页面,列出了我在数据库中的所有员工。在每个员工旁边,您可以单击按钮编辑或删除,这会将该员工的ID添加到url param中:

<%
            List<Employee> employees = (List<Employee>) request.getAttribute("employees");
            if (employees != null)
                for (Employee employee : employees) {
        %>
        <tr>
        <td><%=employee.getId()%></td>
        <td><%=employee.getName()%></td>
        <td><%=employee.getSurname()%></td>
        <td><%=employee.getRegistrationDate()%></td>
        <td><a href="UpdateEmployeeServlet?id=<%=employee.getId()%>">edit</a></td>
        <td><a href="DeleteEmployeeServlet?id=<%=employee.getId()%>">delete</a></td>
    </tr>
    <%
        }
    %>

当我点击&#39;删除&#39;时,该列表会立即被删除,我立即被重定向回我的员工列表,它运作良好。

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = (int) request.getAttribute("id");
    EmployeeDAO.deleteEmployee(id);
    response.sendRedirect("EmployeeServlet");
}

当我点击&#39;编辑&#39;时,我看到te .jsp,其网址包含正确的ID,并要求我提供我的员工的新姓名:

<form action="UpdateEmployeeServlet" method="post">
    Name <input type="text" name="name"><br> 
    Surname <input type="text" name="surname"/><br> 
    <input type="submit"value="Save">

点击&#39; save&#39;后,我收到NullPointerException。好像它无法获取或解析id参数。我的UpdateEmployeeServlet方法中的错误在哪里?

@WebServlet("/UpdateEmployeeServlet")
public class UpdateEmployeeServlet extends HttpServlet {

    public UpdateEmployeeServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    Employee employee = EmployeeDAO.getEmployeeById(id);
    request.getRequestDispatcher("updateemployee.jsp").forward(request, response);
    }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Employee employee = new Employee((int) request.getAttribute("id"), request.getParameter("name"),
                request.getParameter("surname"), Date.valueOf(request.getParameter("registration_date")));
        int status = EmployeeDAO.updateEmployee(employee);
        request.getRequestDispatcher("employeelist.jsp").forward(request, response);

    }
}

2 个答案:

答案 0 :(得分:1)

Integer.parseInt尝试将String转换为Integer,如果未能执行此操作,则会抛出Exception。由于您收到NullPointerException,因此request.getParameter("id")显然是null。原因是您的form不包含name id的所有项目。尝试将hidden input添加到form name="id"并使用正确的值。它应该可以解决你的问题。

答案 1 :(得分:-1)

使用Servlet进行表单处理实际上是低级别的,并且不需要重新发明轮子。您应该考虑使用Spring MVC,它对表单处理有很好的支持。

说完检查<form>的参数。您正在将namesurnamesubmit移交给doPost()。没有给出id。此外,正如已经指出的那样,您使用的是request.getAttribute("id")而不是request.getParameter("id")

您可以将对象设置为doGet() - 方法中的请求范围:request.setAttribute("employee", employee)。然后,您将在JSP中访问此对象的字段。将其添加到您的表单:<input type="hidden" value="${requestScope.employee.id}"/>。您还应该屏蔽您的ID(例如HashIds)并阅读CSRF