Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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:

                         

» View all related tutorials
Related Tags: c class text interface application object io servlet interfaces request context int this response action app war tex to ext

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

Anna S is right. Look like there was a mistake in this example. There is no point to set the local variable to the class field for doing nothing!

Posted by Lan Tran on Monday, 04.28.08 @ 09:30am | #58033

If i overwrote my GetSession.java like below, it would provide d same result. So, what's d point of that class?
By the way, here is citation: '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.'
http://struts.apache.org/2.0.9/struts2-core/apidocs/index.html
---------------------------------------------------
//GetSession.java
package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class GetSession extends ActionSupport{
public String execute() throws Exception{
return SUCCESS;
}
}

Posted by Viraszko on Monday, 08.27.07 @ 15:11pm | #24247

Just a second...
[code]
public void setSession(Map session){
session = this.getSession();
}
[/code/

Are you setting the method's local variable or the class field? Seems like the code should look like:

[code]
public void setSession(Map session){
this.session = session;
}
[/code/

Posted by Anna Skawińska on Monday, 08.13.07 @ 18:36pm | #23298

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.