Setting Attributes into session object

Setting Attributes into session object


Posted in : Servlet Posted on : November 20, 2010 at 4:34 PM Comments : [ 0 ]

In this tutorial you will learn how to set and get attribute in session object

Setting Attributes Into Session

Session attribute is important specially when you are creating and maintaining session session for a client. HttpSession interface provides methods to add, and get attributes into the session so that you can identify the user. An example based upon the above concept is given below

SampleInterfaceImp.java

<HTML>
<HEAD>
<TITLE> Login Page </TITLE>
</HEAD>
<BODY>
<BODY bgcolor=lightblue>
<form name=login method="get" 
action="http://localhost:8080/sessionattribute/attribute">
<center>
<h1>Enter Your Name</h1><br>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userName" value=""></td>
<tr><td align=center><input type="submit" name="Submit" value="Submit" ></td>
<td align=center><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY> 
</HTML>

SampleInterfaceImp.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionAttributeExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String name = request.getParameter("userName");
if (name.equalsIgnoreCase("web")) {
out.println("<Center><h3>");
out.println("Creating A New Session...........");
out.println("</h3></Center>");
HttpSession session = request.getSession(true);
// Setting Attribute to a session
out.println("<Center><h3>");
out.println("Adding Attribute '" + name + "' Into Session");
out.println("</h3></Center>");
session.setAttribute("loggedUser", name);
out.println("<Center><h3>");
out.println("Session Created");
out
.println("<br><a href=\"http://localhost:8080/sessionattribute/display\"
>View Session Attribute in Other Servlet</a>");
out.println("</h3></Center>");
} else {
out.println("<h1>");
out.println("Please enter the name \"web\"");
out.println("<h1>");
}
}

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

DisplaySessionAttribute.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DisplaySessionAttribute extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
HttpSession session = request.getSession(false);
String name = null;
if (session != null) {
name = (String) session.getAttribute("loggedUser");
out.println("<h1><center>");
out.println("Session attribute is -" + name);
out.println("</center><h1>");
}
if (name.equalsIgnoreCase("web")) {
session.removeAttribute("loggedUser");
session.invalidate();
// session.setMaxInactiveInterval(0);
out.println("<h1><center>");
out.println("Session Destroyed........");
out.println("</center><h1>");
} else {
out.println("<h1><center>");
out.println("Session is not Maintained");
out.println("</center><h1>");
}
}

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CreateSession</display-name>
<!-- Servlet -->

<servlet>
<servlet-name>SessionAttributeExample</servlet-name>
<servlet-class>roseindia.net.SessionAttributeExample</servlet-class>
</servlet>

<servlet>
<servlet-name>DisplaySessionAttribute</servlet-name>
<servlet-class>roseindia.net.DisplaySessionAttribute</servlet-class>
</servlet>

<!-- Servlet Mapping-->

<servlet-mapping>
<servlet-name>SessionAttributeExample</servlet-name>
<url-pattern>/attribute</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>DisplaySessionAttribute</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>

<!-- Setting Session Time Out-->

</web-app>

When you run this application it will display message as shown below

 
 
 

Download this example code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics