Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Wednesday, February 21, 2024

Servlet code for use of Cookies by G Krishna

Dear Students,

This is the code for working of the Cookies using the Servlet. 


CookieServlet.java

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import jakarta.servlet.http.Cookie;

import java.io.IOException;


@WebServlet("/CookieSetupServlet")

public class CookieServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

// Creating a new cookie

Cookie cookie = new Cookie("username", "G Krishna");


// Setting cookie path

cookie.setPath("/");


// Setting cookie expiration time - 24 hours (in seconds)

cookie.setMaxAge(24 * 60 * 60);


// Adding the cookie to the response

response.addCookie(cookie);


response.getWriter().println("Cookie has been set.");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

doGet(request, response);

}

}

CookieDisplayServlet.java


import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import jakarta.servlet.http.Cookie; // Import Cookie from jakarta.servlet.http

import java.io.IOException;


@WebServlet("/CookieDisplayServlet")

public class CookieDisplayServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

// Retrieving cookies from the request

Cookie[] cookies = request.getCookies(); // Use Cookie directly without package name

response.setContentType("text/html");

if (cookies != null) {

for (Cookie cookie : cookies) { // Use Cookie directly without package name

if (cookie.getName().equals("username")) {

response.getWriter().println("Cookie Name: " + cookie.getName() + "<br>");

response.getWriter().println("Cookie Value: " + cookie.getValue() + "<br>");

// Deleting the cookie

cookie.setMaxAge(0);

cookie.setPath("/"); // Set to root path

response.addCookie(cookie);

response.getWriter().println("Cookie has been deleted.");

return;

}

}

}

response.getWriter().println("Cookie not found.");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

doGet(request, response);

}

}


Web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="https://jakarta.ee/xml/ns/jakartaee"

xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"

id="WebApp_ID" version="6.0">

<display-name>co</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.jsp</welcome-file>

<welcome-file>default.htm</welcome-file>

</welcome-file-list>

<description></description>

<servlet>

<description></description>

<display-name>CookieServlet</display-name>

<servlet-name>CookieServlet</servlet-name>

<servlet-class>CookieServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CookieServlet</servlet-name>

<url-pattern>/CookieServlet</url-pattern>

</servlet-mapping>

<servlet>

<description></description>

<display-name>CookieDisplayServlet</display-name>

<servlet-name>CookieDisplayServlet</servlet-name>

<servlet-class>CookieDisplayServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CookieDisplayServlet</servlet-name>

<url-pattern>/CookieDisplayServlet</url-pattern>

</servlet-mapping>

</web-app>




Output:



No comments:

Post a Comment