How to access any HTTP session data in Oracle ADF

How to access any HTTP session data in Oracle ADF

If you have ever wondered how to get access to any HTTP session in your ADF application this post will solve the problem.

The first step is to prepare your HttpSessionListener implementation in a View Controller project.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyAdfSessionListener implements HttpSessionListener {
    private static final Map<String, HttpSession> HTTP_SESSIONS = new HashMap<>();

    public MyAdfSessionListener() {
        super();
    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession httpSession = httpSessionEvent.getSession();
        HTTP_SESSIONS.put(httpSession.getId(), httpSession);
        //Uncomment the line below for simple debug
        //System.out.println("Session created:" + httpSession.getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession httpSession = httpSessionEvent.getSession();
        HTTP_SESSIONS.remove(httpSession);
        //Uncomment the line below for simple debug
        //System.out.println("Session destroyed:" + httpSession.getId());
    }

    public static Map<String, HttpSession> getHttpSessions() {
        return Collections.unmodifiableMap(HTTP_SESSIONS);
    }

    public static HttpSession findHttpSession(String sessionId) {
        return HTTP_SESSIONS.get(sessionId);
    }
}

 

After that register it in web.xml  file (1,2,3,4,5)

You can edit web.xml file directly:

<listener>
  <listener-class>com.bettercoding.adf.view.ext.MyAdfSessionListener</listener-class>
</listener>

 

Sessions are stored in HTTP_SESSIONS collection. The listener provides two additional static methods which allows you to access to the session data:

  • getHttpSessions – returns valid (current) sessions collection.
  • findHttpSession – returns session object or null if sessionId does’t exists.

 

The result

Turn on debug by uncommenting System.out lines in the listener. After login you should see something like below

 

Example

Now you are able to handle any creation and destruction of http session in your application by calling:

MyAdfSessionListener.getHttpSessions()
//or
MyAdfSessionListener.findHttpSession("sessionId")

 

The peace of code below prints all active session’s id;

for (HttpSession session: MyAdfSessionListener.getHttpSessions()) {
  //do something with session object
  System.out.println(session.getId());
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Karl P Henselin Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Karl P Henselin
Guest
Karl P Henselin

Seems like
HTTP_SESSIONS.remove(httpSession);
should be
HTTP_SESSIONS.remove(httpSession.getId());
Otherwise quite helpful for the problem I was working on.

Close Menu