错误
错误代码
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// 获取保存在session中登录成功的用户的信息
Student student = (Student)request.getSession().getAttribute("student");
// 获取action
String action = request.getParameter("action");
if (action!=null&&!action.equals("")){
if(action.equals("select")){
String[] selectedIds = request.getParameterValues("selectedId");
if(selectedIds!=null&&selectedIds.length>0){
for (String selectedId : selectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(selectedId));
System.out.println(course);
StudentCourse studentCourse=new StudentCourse(student.getId(),course.getId(),0f);
int i = new StudentCourseDao().insertStudentCourse(studentCourse);
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}else if(action.equals("unselect")){
String[] unselectedIds = request.getParameterValues("unselectedId");
if(unselectedIds!=null&&unselectedIds.length>0){
for (String unselectedId : unselectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(unselectedId));
System.out.println(course);
int i = new StudentCourseDao().deleteById(Integer.parseInt(unselectedId));
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}
原因
以前在使用Servlet的时候,总是在 doGet()或doPost() 中的最后一行才使用请求转发或者重定向。
报异常的原因是重复转发或者重定向了请求。
解决
如果能在if条件中进行跳转就不需要在最后一行使用请求转发或者重定向。
或者在if中使用return语句,而最后一个请求转发或重定向不需要使用return语句。
正确代码
解决代码1
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// 获取保存在session中登录成功的用户的信息
Student student = (Student)request.getSession().getAttribute("student");
// 获取action
String action = request.getParameter("action");
if (action!=null&&!action.equals("")){
if(action.equals("select")){
String[] selectedIds = request.getParameterValues("selectedId");
if(selectedIds!=null&&selectedIds.length>0){
for (String selectedId : selectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(selectedId));
System.out.println(course);
StudentCourse studentCourse=new StudentCourse(student.getId(),course.getId(),0f);
int i = new StudentCourseDao().insertStudentCourse(studentCourse);
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}else if(action.equals("unselect")){
String[] unselectedIds = request.getParameterValues("unselectedId");
if(unselectedIds!=null&&unselectedIds.length>0){
for (String unselectedId : unselectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(unselectedId));
System.out.println(course);
int i = new StudentCourseDao().deleteById(Integer.parseInt(unselectedId));
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}
}
}
删除了最后一行的请求转发。
解决代码2
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// 获取保存在session中登录成功的用户的信息
Student student = (Student)request.getSession().getAttribute("student");
// 获取action
String action = request.getParameter("action");
if (action!=null&&!action.equals("")){
if(action.equals("select")){
String[] selectedIds = request.getParameterValues("selectedId");
if(selectedIds!=null&&selectedIds.length>0){
for (String selectedId : selectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(selectedId));
System.out.println(course);
StudentCourse studentCourse=new StudentCourse(student.getId(),course.getId(),0f);
int i = new StudentCourseDao().insertStudentCourse(studentCourse);
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
return;
}
}else if(action.equals("unselect")){
String[] unselectedIds = request.getParameterValues("unselectedId");
if(unselectedIds!=null&&unselectedIds.length>0){
for (String unselectedId : unselectedIds) {
Course course = new CourseDao().checkCourseById(Integer.parseInt(unselectedId));
System.out.println(course);
int i = new StudentCourseDao().deleteById(Integer.parseInt(unselectedId));
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
return;
}
}
request.getRequestDispatcher("/pageServlet?action=pager&user=student&tab=select_course").forward(request,response);
}
}
在if条件中添加了return语句。