Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Store user data from registration form in cookies  RSS feed

Navy Akki
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am new to JSP and I have to create a registration form, so when the user fills out all the information and clicks submit, the information should be stored in cookies. I cannot use database to store user information. Also after the user registers, he/she has to login and I have to validate the username and password with the user information stored in cookies. I have searched online for solution but couldnt find any!, Please Help
Here's the code for Register.jsp:


Bear Bibeault
Author and ninkuma
Posts: 65830
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason for this bizarre requirement? You do realize that any cookies stored will not be available to any other browser, or any other system, and may even disappear when the browser is closed?
Alex Theedom
Ranch Hand
Posts: 77
Java Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that this way is not recommended practice. Nevertheless here is a proposed solution.

I created two new files. A servlet and a LoginCheck.jsp page.

1. The servlets responsibility is to retrieve the username and password from the request object and store them in the cookie. It then redirects to the login.jsp page.
2. The LoginCheck.jsp page verifies that the user details are correct and deals with the result appropriately.

Heres the getPost method code:

Both the username and password are delimited by a colon (line 4) and stroed in the same cookie called loginDetails (line 6). This saves setting two cookies. The servlet redirects to the index.jsp login page (line 10).

NOTE: Don't use a requestDispatcher to forward to the JSP page like so

because this just forwards the request and response objects. A new request must be created by the browser.

The LoginCheck.jsp uses Expression Language and tag libs to access and process the cookie and therefore you need to include the taglibs:

The value stored in the cookie is retrieved and stores in the pageScope in order to be split into the username and password (lines 1 and 2).

The username and password are tested against those entered by the user (line 3) and if match the user sees a Welcome message otherwise s/he is redirected to the login page (lines 5 to 12).

No changes are required on the index.jsp page nor the Register.jsp pages.

Normally a servlet would verify the username and password against a database. This method is not secure as the cookie could be deleted, forcing the user to register again.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!