S04L03 – Read and Write operation of Cookie in JSP and Servlets

Read and Write Operations of Cookies in JSP and Servlets

Table of Contents

1. Introduction

In modern web applications, maintaining user sessions is critical. Cookies provide a simple, efficient way to store small data snippets on the client side. In Java-based web development, using JSP (JavaServer Pages) and Servlets, cookies are often utilized to manage user sessions, remember preferences, and personalize content. This article covers cookie operations in JSP and Servlets—how to create, read, and delete cookies in your web applications.

We’ll walk through cookie basics, key functions in JSP and Servlets, and provide practical examples to help you understand how to implement cookie-based features in your web app.

2. Understanding Cookies in JSP and Servlets

A cookie is a small piece of data sent from the server to the client, stored on the user’s browser. In Java, JSP and Servlets allow developers to interact with cookies using the javax.servlet.http.Cookie class. Cookies are ideal for persisting user-specific data such as authentication tokens, session identifiers, and user preferences.

Cookie Features:

  • Persistent storage across sessions.
  • Managed client-side, minimizing server load.
  • Can store small amounts of data (up to 4 KB).

Cookies follow a lifecycle, from creation to deletion. This process includes:

  1. Creation: Server generates a cookie and sends it to the client’s browser.
  2. Storage: The client stores the cookie and returns it with subsequent requests.
  3. Retrieval: The server reads cookies from incoming HTTP requests.
  4. Modification/Deletion: The server can update or delete cookies based on requirements.

4. How to Create, Read, and Delete Cookies

In JSP and Servlets, cookies are manipulated using the Cookie class. Here’s a breakdown of key operations:

Operation Description
Create Create a new cookie and send it to the client.
Read Retrieve cookies from the client’s request.
Delete Invalidate or remove cookies.

Syntax:

1. Creating a Cookie:

2. Reading a Cookie:

3. Deleting a Cookie:

5. Code Explanation: Read and Write Cookie Operations

Now let’s explore a practical implementation from the project files.

Example Code (SiteController.java):

Code Explanation:

  • Session Management: The user credentials are validated, and if successful, an old session is invalidated, and a new session is created with a timeout of 500 seconds.
  • Cookie Creation: A new cookie username is created, storing the username value. This cookie is then added to the response, which sends it to the client.
  • Redirection: Based on the login result, the user is either redirected to the dashboard or back to the login page.

Output:

If login is successful, the browser stores a cookie named username, which can be used to personalize the user experience.

6. Conclusion

Cookies play a vital role in maintaining user sessions and storing personalized data in web applications. Using JSP and Servlets, creating, reading, and deleting cookies is straightforward. By understanding the lifecycle of cookies and how they interact with the server-client architecture, developers can implement robust, user-centric web applications.