一般这种错误是发送id的时候,servlet接收不到。
<a href=”./deleteServlet?id=${emp.id}” style=”color:#000;”>删除</a>
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import edu.jmi.emp.dao.EmpDao;
/**
* Servlet implementation class deleteServlet
*/
@WebServlet(“/deleteServlet”)
public class deleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public deleteServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append(“Served at: “).append(request.getContextPath());
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding(“utf-8”);
int id=Integer.parseInt(request.getParameter(“id”));
EmpDao empDao=new EmpDao();
boolean b=empDao.deleteEmp(id);
System.out.println(id);
if(b==true) {
request.getRequestDispatcher(“MainServlet”).forward(request, response);
}else {
response.sendRedirect(“index.jsp”);
}
}
}