在做小练习时有一次出现http500错误,查询上一次的错误,虽然类似但是有不同
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:542)
java.lang.Integer.parseInt(Integer.java:615)
com.bjpowernode.controller.AddStudentServlet.doGet(AddStudentServlet.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
检查类型转换也没有问题,通过仔细阅读错误信息发现我的前端代码本来是通过post请求方式传递参数,但是错误信息却显示"doget"方法
com.bjpowernode.controller.AddStudentServlet.doGet(AddStudentServlet.java:22)
重新回去检查代码,发现把应该写在dopost方法中的代码写进了doget中
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strName = request.getParameter("name");
String strAge = request.getParameter("age");
String config = "application-context.xml";
ApplicationContext atc = new ClassPathXmlApplicationContext(config);
StudentService studentService = (StudentService) atc.getBean("studentService");
Student student = new Student();
student.setStuage(Integer.parseInt(strAge));
student.setStuname(strName);
studentService.addStudent(student);
request.getRequestDispatcher("/show.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
在dopost中也没能重写doget方法导致的,修改代码后解决问题。