0% found this document useful (0 votes)
9 views12 pages

ajp_prac_23

Uploaded by

saeedarwatkar
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)
9 views12 pages

ajp_prac_23

Uploaded by

saeedarwatkar
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/ 12

Practical No.

23
1) Develop a servlet program to display various details about session using HttpSession
methods.

i) Html file:
<!DOCTYPE html>

<html>

<head>

<title>Session Details</title>

</head>

<body>

<h2>Session Details Page</h2>

<form method="post" action="SessionDetailsServlet">

Enter your name: <input type="text" name="username" required><br><br>

<input type="submit" value="Start Session">

</form>

</body>

</html>

ii) Servlet file:


import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/SessionDetailsServlet")
public class SessionDetailsServlet extends HttpServlet

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

HttpSession ses = req.getSession();

String username = req.getParameter("username");

ses.setAttribute("username", username);

pw.println("<html><body>");

pw.println("<h2>Session Details</h2>");

pw.println("<p><b>Username (from session):</b> " + ses.getAttribute("username") + "</p>");

pw.println("<p><b>Session ID:</b> " + ses.getId() + "</p>");

pw.println("<p><b>Session Creation Time:</b> " + new Date(ses.getCreationTime()) + "</p>");

pw.println("<p><b>Last Accessed Time:</b> " + new Date(ses.getLastAccessedTime()) + "</p>");

pw.println("<p><b>Max Inactive Interval:</b> " + ses.getMaxInactiveInterval() + " seconds</p>");

pw.println("<h3>All Session Attributes:</h3>");

ses.removeAttribute("username");

pw.println("<p><b>Username removed from session.</b></p>");

if (ses.getAttribute("username") == null)

pw.println("<p><i>Username attribute is no longer available in the session.</i></p>");

pw.println("</body></html>");

pw.close();

}
Output:
2) Develop a program to display the last accessed time of session and expire session with
specified time.

i) Html file:
<!DOCTYPE html>

<html>

<head>

<title>Session Details</title>

</head>

<body>

<h2>Session Details Page</h2>

<form method="post" action="Sessionservlet2">

Enter your name: <input type="text" name="username" required><br><br>

<input type="submit" value="Start Session">

</form>

</body>

</html>

ii) Servlet file:


import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/Sessionservlet2")

public class Sessionservlet2 extends HttpServlet {


protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

String u1 = req.getParameter("username");

HttpSession ses = req.getSession();

ses.setAttribute("username", u1);

ses.setMaxInactiveInterval(30); // 30 seconds

String sId = ses.getId();

long creationTime = ses.getCreationTime();

long lastAccessedTime = ses.getLastAccessedTime();

pw.println("<html><body>");

pw.println("<h2>Session Management</h2>");

pw.println("<p><b>Last Accessed Time:</b> " + new Date(lastAccessedTime) + "</p>");

pw.println("<p><b>Session Timeout:</b> Session will expire after 30 seconds of inactivity.</p>");

pw.println("<p><b>Note:</b> If you refresh the page after 30 seconds of inactivity, the session will be
invalidated.</p>");

pw.println("</body></html>");

pw.close();

iii) Web.xml:
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<session-config>

<session-timeout>

30
</session-timeout>

</session-config>

<servlet>

<servlet-name>Sessionservlet2</servlet-name>

<servlet-class>Sessionservlet2</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Sessionservlet2</servlet-name>

<url-pattern>/Sessionservlet2</url-pattern>

</servlet-mapping>

</web-app>

Output:
3) Write the output of the following code:

i) Html file:
<!DOCTYPE html>

<html>

<head>

<title>Session Details</title>

</head>

<body>

<h2>Session Output Page</h2>

<form method="post" action="Sessionservlet3">

Enter your name: <input type="text" name="username" required><br><br>

<input type="submit" value="Go">

</form>

</body>

</html>

ii) Servlet file:


import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/Sessionservlet3")

public class Sessionservlet3 extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {


res.setContentType("text/html");

PrintWriter pw = res.getWriter();

String u1 = req.getParameter("username");

pw.println("Welcome: "+u1);

HttpSession ses = req.getSession();

ses.setAttribute("username", u1);

pw.close();

Output:
4) Write a program to session id and session time in browser window.

i) Html file:
<!DOCTYPE html>

<html>

<head>

<title>Session Details</title>

</head>

<body>

<h2>Session Information</h2>

<form method="post" action="Sessionservlet4">

<input type="submit" value="Get Session Info">

</form>

</body>

</html>

ii) Servlet file:

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.annotation.*;

import javax.servlet.http.*;

@WebServlet("/Sessionservlet4")

public class Sessionservlet4 extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {


res.setContentType("text/html");

PrintWriter pw = res.getWriter();

String u1 = req.getParameter("username");

pw.println("Welcome: "+u1);

HttpSession ses = req.getSession();

String sessionId = ses.getId();

long creationTime = ses.getCreationTime();

Date creationDate = new Date(creationTime);

pw.println("<h2>Session Details</h2>");

pw.println("<p><b>Session ID:</b> " + sessionId + "</p>");

pw.println("<p><b>Session Creation Time:</b> " + creationDate + "</p>");

pw.close();

Output:

You might also like