Preserving session in Java with sendredirect

前端 未结 4 1168
慢半拍i
慢半拍i 2020-12-16 16:49

I am creating a Login Application in JAVA.I am making the presentation in JSP and all the logic (Database connectivity) in Servlet [this is not a right approach I know that]

相关标签:
4条回答
  • 2020-12-16 16:57

    Use the RequestDispatcher and set your username variable using request.setAttribute(). In this case the dispatcher will not create a new request but the same request will be forwarded using the forward() method.

    0 讨论(0)
  • 2020-12-16 17:00

    Some browsers like Chromium for instance do not allow cookies from localhost or IP addresses, so the session cannot be preserved and changes on every refresh. This can be easily checked with the browser developer tools that show all cookies of the request.

    For development, set up your workstation to resolve some more serious name (like host.kitty.com) into localhosts. Under Linux, just add entry to /etc/hosts.

    0 讨论(0)
  • 2020-12-16 17:07

    Try to edit your tomcat context.xml file and replace <Context> tag to <Context useHttpOnly="false"> , this helped me.

    0 讨论(0)
  • 2020-12-16 17:13

    For the redirected request to come back and attach to the same session, it needs a session ID, usually carried in a JSESSIONID (or another name) cookie or in the URL as a parameter.

    This cookie or URL parameter should be added by the servlet container and you should not have to add it yourself.

    If you do not see the cookie in your browser, and you are not attaching the JSESSIONID to the URL, then it is creating a new session with each request, and not attaching to the same session.

    0 讨论(0)
提交回复
热议问题