我在调试器中运行了代码,并确认该对象是在 Java 代码中创建的,并且在 JSP 中为 null。为什么 JSP 使用新 session ?
在调试器中,它进入 Java 代码并在具有 id 的 session 中设置验证码。当我运行 JSP 时,它获取具有不同 id 的 session ,失败,然后进入 doGet() 并使用新的验证码对象设置当前 id session 。将验证码存储在 session 中,但 JSP 运行时并未使用该 session 。
这里是一些代码片段
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);
}
}
最佳答案
我发现了我的问题。在 JSP 的顶部, session 已失效。我删除了 session.invalidate() 现在它可以工作了。
关于java - session属性在Java中被设置为一个对象,但在JSP中它是空的。为什么 JSP 使用新 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38206874/