簡體   English   中英

session屬性在Java中設置為對象,但在JSP中為null。 為什么JSP使用新會話?

[英]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?

我在調試器中運行了代碼,並確認該對象正在Java代碼中創建,並且在JSP中為空。 為什么JSP使用新會話?

在調試器中,它將進入Java代碼並在具有ID的會話中設置驗證碼。 當我運行JSP時,它將獲得具有不同ID的會話,然后失敗,然后進入doGet()並使用新的驗證碼對象設置當前ID會話。 在一個會話中存儲驗證碼,但是在運行JSP時未使用該會話。

這是一些代碼片段

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.invalidate(),它現在可以工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM