0% found this document useful (0 votes)
38 views4 pages

Session Management

The document contains a simple web application that demonstrates session management using servlets in Java. It includes an HTML form for users to enter their name, which is then stored in a session by the SetSessionServlet, and a second servlet, GetSessionServlet, that retrieves and displays the stored name. The application is configured using a web.xml file for servlet mapping.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Session Management

The document contains a simple web application that demonstrates session management using servlets in Java. It includes an HTML form for users to enter their name, which is then stored in a session by the SetSessionServlet, and a second servlet, GetSessionServlet, that retrieves and displays the stored name. The application is configured using a web.xml file for servlet mapping.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

DOCTYPE html>

<html>

<head>

<title>Session Management Example</title>

</head>

<body>

<h2>Enter Your Name</h2>

<form action="setSessionServlet" method="POST">

<label>Your Name: </label>

<input type="text" name="userName"><br><br>

<input type="submit" value="Save Name">

</form>

</body>

</html>

import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class SetSessionServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {

// Get the name entered by the user

String userName = request.getParameter("userName");

// Get the session object

HttpSession session = request.getSession();

// Store the user's name in the session

session.setAttribute("userName", userName);

// Set content type for response

response.setContentType("text/html");

// Output response to confirm the name has been saved in the


session

PrintWriter out = response.getWriter();

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

out.println("<h2>Hello, " + userName + "! Your name has been


saved in the session.</h2>");

out.println("<a href='getSessionServlet'>Click here to see your


saved name from the session.</a>");

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

import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class GetSessionServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Get the session object

HttpSession session = request.getSession(false); // Don't create a


new session if one doesn't exist

String userName = null;

// Check if the session exists and retrieve the "userName" attribute

if (session != null) {

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

// Set content type for response

response.setContentType("text/html");

// Output response

PrintWriter out = response.getWriter();

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

if (userName != null) {

out.println("<h2>Welcome back, " + userName + "!</h2>");

} else {

out.println("<h2>No session found. Please enter your name


first.</h2>");

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

}
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<servlet>

<servlet-name>SetSessionServlet</servlet-name>

<servlet-class>SetSessionServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SetSessionServlet</servlet-name>

<url-pattern>/setSessionServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>GetSessionServlet</servlet-name>

<servlet-class>GetSessionServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GetSessionServlet</servlet-name>

<url-pattern>/getSessionServlet</url-pattern>

</servlet-mapping>

</web-app>

You might also like