tags:

views:

185

answers:

1

Hi,

I am having issues with session in my java code. Upon submitting a form via post, the java servlet will determine if the captcha is correct. May I know anything that I should add to use the session in java servlet? Is there any thing that I need to import to use session?

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
    // Validate Captcha
    String userCaptcha = request.getParameter("captcha");
    Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
    if (!captcha.isCorrect(userCaptcha)) {
        errorMsgs.add("Please input the correct Captcha value.");
    }
} catch (RuntimeException e) {
    ...
}
...

Thank you very much.

+4  A: 

Well you'll need:

// create session if one doesn't exist
HttpSession session = request.getSession(true); 

You're not actually referencing a session anywhere in your code.

cletus
Do I need to include HttpSession on "protected doPost(HttpServletRequest request, HttpServletResponse response)"?
jl
You'll need to import javax.servlet.HttpSession but javac will complain if you don't do that anyway.
cletus
Thanks a lot, that works.
jl