Home Struts Struts2 Strutsresources Accessing Session Object

Related Tutorials


 
 

Share on Google+Share on Google+

Accessing Session Object

Advertisement
In this section, we will develop a simple application to access the framework resources like the session object, session context and the last accessed session time.

Accessing Session Object

     

In this section, we will develop a simple application to access the framework resources like the session object, session context and the last accessed session time. To access the session, you need an action class implementing the SessionAware interface and extending ActionSupport class

org.apache.struts2.interceptor.SessionAware  Interface: Actions that need access to the user's HTTP session should implement this interface. This interface is only relevant if the Action is used in a servlet environment. Note that using this interface makes the Action tied to a servlet environment, so it should be avoided if possible since things like unit testing will become more difficult.

Description:

add the following action snippet to the following struts.xml file. 

struts.xml

<action name="GetSession" class="net.roseindia.GetSession">
   <result>/pages/staticparameter/GetSession.jsp</result>
</action>

Now, create a JavaBean (GetSesstion.java). This is a simple POJO (Plain Old Java Object). Here, we implement SessionAware interface. 

The setSession() method sets the session in a Map object. It is called at the running time.

GetSession.java

package net.roseindia;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class GetSession extends ActionSupport implements SessionAware{

  private Map session;
  public String execute() throws Exception{
  return SUCCESS;
  }

  public void setSession(Map session){
  session = this.getSession();
  }

  public Map getSession(){
  return session;
  }

}

Now, we create a jsp page for viewing the session object, session context and session time. The session object provides information about the session, getSessionContext() method provides the information about the session context and getLastAccessedTime() provides date and time when the last session is accessed. 

GetSession.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@page language="java" import="java.util.*" %>
<html>
  <head>
  <title>Get Session Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc"> Session Example! </span></h1>
  <b>Session:</b><%=session%><br>
  <b>Session Context: </b><%=session.getSessionContext() %><br>
  <b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>
  
  </body>
</html> 

Finally, open the web-browser and type the http://localhost:8080/struts2tutorial/roseindia/GetSession.action in the address bar. It displays the information about framework session, session context and last accessed session time.

Output of this application:

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 12, 2007

Related Tutorials

Discuss: Accessing Session Object   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Deepika
June 22, 2011
session handling

how to validate the username also include session
ekta
February 24, 2012
accessing session object in struts2

here the given example is not correct because here you are accessing session which is created by jsp. here session object we are created does not matter.If you run the jsp only than also it will display the same content because that session was created by jsp when it loads not by us.
DMCA.com