[go: up one dir, main page]

0% found this document useful (0 votes)
6 views2 pages

Lab Assignment 3

The document provides a Java servlet example that demonstrates how to create both persistent and non-persistent cookies on the client side. A session cookie named 'SessionUser' is created without an expiration date, while a persistent cookie named 'PersistentUser' is set to expire after 7 days. The servlet can be deployed on a Java EE-compatible server like Apache Tomcat and displays a confirmation message upon access via a browser.

Uploaded by

suvadeeppal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Lab Assignment 3

The document provides a Java servlet example that demonstrates how to create both persistent and non-persistent cookies on the client side. A session cookie named 'SessionUser' is created without an expiration date, while a persistent cookie named 'PersistentUser' is set to expire after 7 days. The servlet can be deployed on a Java EE-compatible server like Apache Tomcat and displays a confirmation message upon access via a browser.

Uploaded by

suvadeeppal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSE 3rd Year 5th Sem, Sec- C & D (2025-26)

Lab/ Practical’s details:


3. Write a program using servlet to write persistent and non-persistent cookies
on client side.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/CookieDemo")
public class CookieDemo extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

// Create a non-persistent (session) cookie


Cookie sessionCookie = new Cookie("SessionUser", "Alice");
// No expiry set — will be deleted when browser closes
response.addCookie(sessionCookie);

// Create a persistent cookie (valid for 7 days)


Cookie persistentCookie = new Cookie("PersistentUser", "Bob");
persistentCookie.setMaxAge(7 * 24 * 60 * 60); // 7 days in seconds
response.addCookie(persistentCookie);
response.setContentType("text/html");
response.getWriter().println("<html><body>");
response.getWriter().println("<h2>Cookies have been set!</h2>");
response.getWriter().println("<p>Session Cookie:
SessionUser=Alice</p>");
response.getWriter().println("<p>Persistent Cookie: PersistentUser=Bob
(expires in 7 days)</p>");
response.getWriter().println("</body></html>");
}
}
------------------------------------------------------------------------------------------------
• Session Cookie: SessionUser=Alice — no expiration, deleted when
browser closes.
• Persistent Cookie: PersistentUser=Bob — expires after 7 days (depends).
You can deploy this servlet in a Java EE-compatible server like Apache Tomcat.
When accessed via browser, it sets both cookies on the client side.

You might also like