Search code examples
javaservletssession-cookies

When use sessions in Servlet?


I need to create a Simple Web Application using Sessions and Cookies with Servlet. I have two pages, Login page and Registration page (a simple form), both are HTML pages. Have I to use Session only for Sign-in or only for Sign-up or both case? And why? Thank you.


Solution

  • If you need to transfer data from one page to another page with different requests,you can use session.

    If you just submit the information from sign-up page and then store the data into database,you need not to use session.

    If you need when user sign-up the page go to the sign-in page and show user's name,you can use session like this session.setAttribute("username",request.getParameter("userName")) and in other requests you can get it likesession.getAttribute("username") and send it to page to show who sign-in.