【发布时间】:2016-11-07 11:51:05
【问题描述】:
我在调试器中运行代码并确认对象是在 Java 代码中创建的,并且在 JSP 中为空。为什么 JSP 使用新会话?
在调试器中,它进入 Java 代码并在带有 id 的会话中设置验证码。当我运行 JSP 时,它获取具有不同 ID 的会话,失败,然后进入 doGet() 并使用新的验证码对象设置当前 ID 会话。将验证码存储在会话中,但 JSP 运行时未使用该会话。
这里有一些代码sn-ps
Java:
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
Captcha captcha = new Captcha.Builder(_width, _height).
addText().addNoise().
addBackground(new BrightGradiatedBackgroundProducer()).
build();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute("simpleCaptcha", captcha); // object is getting set
}
JSP:
session=request.getSession(false);
if (session==null)
session=request.getSession(true);
boolean isCaptchaTrue = false;
String strCaptcha = request.getParameter("captcha");
String captchaType = request.getParameter("captchaType");
if (strCaptcha != null && captchaType != null) {
if(session.getAttribute("simpleCaptcha") instanceof Captcha){
Captcha captcha = (Captcha) session.getAttribute("simpleCaptcha");
isCaptchaTrue = captcha.isCorrect(strCaptcha);
}else if(session.getAttribute("simpleCaptcha") instanceof AudioCaptcha){
AudioCaptcha captcha = (AudioCaptcha) session.getAttribute("simpleCaptcha");
isCaptchaTrue = captcha.isCorrect(strCaptcha);
}
}
【问题讨论】:
-
会不会和这个有关? stackoverflow.com/questions/17419727/…