How to use Session in Java Servlet?

I'm having session problems in my java code. After submitting the form via mail, the java servlet will determine if it is converted correctly. Can I find out something I need to add in order to use a session in a Java servlet? Is there something I need to import to use the 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) {
    ...
}
...

Many thanks.

+3
source share
1 answer

Well you need:

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

You are not actually referring to a session anywhere in your code.

+6
source

Source: https://habr.com/ru/post/1728013/


All Articles