try { RandomValidateCode randomValidateCode = new RandomValidateCode(); String randomString = randomValidateCode.getRandcode(req, resp);//输出图片方法 // Cannot create a session after the response has been committed HttpSession session = req.getSession(); session.setAttribute(RandomValidateCode.RANDOMCODEKEY, randomString); } catch (Exception e) { logger.error(e.getMessage(), e); }
如上生成并且存储验证码的代码,在登录页面 url 不输工程名的时候(如 http://127.0.0.1:7878/),会报异常 Cannot create a session after the response has been committed
因此需要把 session.setAttribute(RandomValidateCode.RANDOMCODEKEY, randomString); 在 response 写成验证码图片前执行