This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Store user data from registration form in cookies  RSS feed

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:


Author and ninkuma
Posts: 66789
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?
Ranch Hand
Posts: 114
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.

Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!