Cannot forward after response has been committe

问题描述

private void login(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, ServletException {// TODO Auto-generated method stubString username=req.getParameter("username");String password=req.getParameter("password");String captcha=req.getParameter("captcha");String captcha1=(String)req.getSession().getAttribute("captcha");if(!captcha.equals(captcha1)){req.setAttribute("error","验证码错误");req.getRequestDispatcher("/security/login.jsp").forward(req, resp);}User user=userDao.login(username,password);if(user!=null){user.setLast_login(new Date());userDao.update(user);req.getSession().setAttribute("user", user);req.getRequestDispatcher("index.jsp").forward(req, resp);}else{req.setAttribute("error", "用户名或密码错误");req.getRequestDispatcher("/security/login.jsp").forward(req,resp);}}第一次登录 输入错误的用户名密码 能成功跳转回去 提示错误。登录成功之后,再一次返回login.jsp界面 输入一个错误的用户名密码 这时报错:java.lang.IllegalStateException: Cannot forward after response has been committed但是输入正确的用户名密码却一切正常。起初我以为是成功后跳转使用了response 但是我改成了request之后也一样出错?

解决方案

引用private void login(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, ServletException {// TODO Auto-generated method stubString username=req.getParameter("username");String password=req.getParameter("password");String captcha=req.getParameter("captcha");String captcha1=(String)req.getSession().getAttribute("captcha");if(!captcha.equals(captcha1)){req.setAttribute("error","验证码错误");req.getRequestDispatcher("/security/login.jsp").forward(req, resp);//////////////////////////////return; //登入出错就返回,不能让其继续运行///////////////////////////////}User user=userDao.login(username,password);if(user!=null){user.setLast_login(new Date());userDao.update(user);req.getSession().setAttribute("user", user);req.getRequestDispatcher("index.jsp").forward(req, resp);}else{req.setAttribute("error", "用户名或密码错误");req.getRequestDispatcher("/security/login.jsp").forward(req,resp);}}
解决方案二:
成功后跳转使用了response 就会出这个错! 怪异! 我也很期待看下你这个到底啥问题哈!

时间: 2024-09-29 05:39:35

Cannot forward after response has been committe的相关文章

J2EE中RequestDispatcher.forward()和response.sendRedirect()的区别详谈

一.RequestDispatcher.forward() 在服务器端运行.采用采用请求转发,request对象始终存在,不会重新创建,前后页面共享同一个request.重定向后浏览器地址栏URL不变. public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html

Servlet中报Cannot forward after response has been committed错

一.遇到问题 punlic void go() { if(a) request.getRequestDispatcher("/a.jsp").forward(request, response); if (b) request.getRequestDispatcher("/b.jsp").forward(request, response); } 执行该方法会报Cannot forward after response has been committed 二.查找

jsp/servlet 中sendRedirect、include和forward区别

sendRedirect response.sendRedirect(); 服务器根据逻辑,发送一个状态码,告诉浏览器重新去请求新的地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取.注意:使用request.setAttribute的内容,不能读取了.因为不是同一个请求了.如图所示 2  forward (1) 在servlet里面 request.getRequestDispatcher("jsp2.jsp").forward(re

JSP中forward()与sendRedirect()的区别

jsp的页面跳转有两种方法:RequestDispatcher.forward()方法和HttpServletResponse.sendRedirect()方法. 它们的区别是: RequestDispatcher.forward()方法仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址,他是不会改变Request的值,如果需要在下一个页面中能从中获 取新的信息的话,可以Request.setAttribute()来放置一些标志,这样从下一个页面中获取.其写法如下: try{

【JSP开发】response重定向和resquest转发

response重定向 package cn.edu.Response; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSer

Ajax request response 乱码解决方法_AJAX相关

此功能已进入试用一周了,都没有问题,奇怪的是今天出现了问题?? 复制代码 代码如下: AjaxRequestObj.createEqStatusParameter=function() {     if (eqStatusArray.length < 1) {         return;     }     var paramNameArray = new Array();     var paramValueArray = new Array();     //取状态数组     for

JavaWeb 后端 &lt;三&gt; 之 Response Request 学习笔记

一.响应对象 Response(重点:HTTP协议响应部分)   查看 HttpServletResponse set是 有这个头 修改 没有添加一个新的 和 add 有点小区别 案例: 1. 字节流向客户端输出中文数据 ServletOutputStream //字节流向客户端输出中文数据 public class ResponseDemo1 extends HttpServlet {           protected void doGet(HttpServletRequest requ

【JSP开发】response的页面定时跳转和控制浏览器缓存

页面定时跳转 package cn.edu.Response; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.

jsp中forward和redirect的区别(转)

一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(request, response);   //转发到new.jsp response.sendRedirect("new.jsp");   //重定向到new.jsp 在jsp页面中你也会看到通过下面的方式实现转发: <jsp:forward page="apage.jsp"