Programming Pandit

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


Latest Update

Wednesday, February 21, 2024

SESSION TRACKING IN SERVLET by G Krishna


SessionSetupServlet.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.HttpSession;

import java.io.IOException;


@WebServlet("/SessionSetupServlet")

public class SessionSetupServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Create a new session or get the existing session

HttpSession session = request.getSession();


// Set session attributes

session.setAttribute("username", "G Krishna");

session.setAttribute("email", "cse.gopalkrishna@gmail.com");


response.getWriter().println("Session has been set up.");

}

}



SessionDisplayServlet.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.HttpSession;

import java.io.IOException;


@WebServlet("/SessionDisplayServlet")

public class SessionDisplayServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Get the session

HttpSession session = request.getSession();


// Get session attributes

String username = (String) session.getAttribute("username");

String email = (String) session.getAttribute("email");


response.setContentType("text/html");

response.getWriter().println("Session Attributes:<br>");

response.getWriter().println("Username: " + username + "<br>");

response.getWriter().println("Email: " + email + "<br>");

}

}



SessionRemoveServlet.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.HttpSession;

import java.io.IOException;


@WebServlet("/SessionRemoveServlet")

public class SessionRemoveServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Get the session

HttpSession session = request.getSession();


// Remove session attributes

session.removeAttribute("username");

session.removeAttribute("email");


// Invalidate the session

session.invalidate();


response.getWriter().println("Session attributes have been removed and session has been invalidated.");

}

}




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>se</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>

<servlet>

<description></description>

<display-name>SessionSetupServlet</display-name>

<servlet-name>SessionSetupServlet</servlet-name>

<servlet-class>SessionSetupServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SessionSetupServlet</servlet-name>

<url-pattern>/SessionSetupServlet</url-pattern>

</servlet-mapping>

<servlet>

<description></description>

<display-name>SessionDisplayServlet</display-name>

<servlet-name>SessionDisplayServlet</servlet-name>

<servlet-class>SessionDisplayServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SessionDisplayServlet</servlet-name>

<url-pattern>/SessionDisplayServlet</url-pattern>

</servlet-mapping>

<servlet>

<description></description>

<display-name>SessionRemoveServlet</display-name>

<servlet-name>SessionRemoveServlet</servlet-name>

<servlet-class>SessionRemoveServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SessionRemoveServlet</servlet-name>

<url-pattern>/SessionRemoveServlet</url-pattern>

</servlet-mapping>

</web-app>



Output:









No comments:

Post a Comment