Talk is cheap. Show me the code. ~ Linus Torvalds
A browser cookie is a small piece of data sent from website and stored in user’s web browser while the user is browsing a given website. Every time user loads the website, browser sends the cookie back to the server to notify the website of the user’s previous activity.
We are going to learn how to set up cookies with servlet. We are going to create three files:
- HelloForm.java (servlet).
- ReadCookie.java (servlet).
Creating the first Servlet which is HelloForm.java, here is the code:
Creating HelloForm.html here is the code
Now I want you to run HelloForm.html on your server and you will get something like this:
Now I need you to enter First Name and Last Name. After that, click submit. Now we need a way to read the stored names. I am going to use First Name as Henry and Last Name as Mbugua. Lastly, we will create Servlet to read our names.
Creating ReadCookie.java, here is the code:
Now we will run ReadCookie.java in our server and see if we can read our stored names. Here is my result:
Have something to add, leave me a comment. Happy coding!