【问题标题】:session attribute is being set to an object in Java, but it is null in the JSP. Why is the JSP using a new session?session 属性在 Java 中设置为对象,但在 JSP 中为空。为什么 JSP 使用新会话?
【发布时间】: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);
  }
} 

【问题讨论】:

标签: java jsp session servlets


【解决方案1】:

我发现了我的问题。在 JSP 的顶部,会话正在失效。我删除了 session.invalidate(),它现在可以工作了。

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 2013-03-26
    • 2012-09-09
    • 2012-07-07
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多