aspose file tools*
The moose likes Servlets and the fly likes filter or listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "filter or listener" Watch "filter or listener" New topic
Author

filter or listener

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a servlet-listener to obtain infos about the Users session.

How can I retrieve the ServletRequest from this listener to obtain e.g. IP-Adress (getLocalAddr).

Something like:



Is a filter better for this?
[ October 13, 2008: Message edited by: nimo frey ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The Request is not available to the listeners because they don't get called on a per-request basis.

The filter is called on a per-request basis for all the URLs it is mapped to, so you do get the Request object, so it is a better place for this.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

What is it you are trying to do with this listener or filter?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a listener who stores sessions beeing created (time of, HTTPSession) into a Map.

I also need the the methods of the ServletRequest.

So I ask, if I should only use a filter, as I can also get the Session (SessionCreationTime, HTTPSession,..) from the methods of ServletRequest.

For what do I need a listener, like:



when I can get the HTTPSession also from a filter?

Should I delete my Listener and only use a filter?

I am also considering to use the clickstream-api
http://www.opensymphony.com/clickstream/ and found out that the clickstream use both listener and filters:

# Start tracking click stream when users' sessions begin (via a Listener).
# Track information with each click the user makes (via a Servlet Filter).


Should I use this api? Or should I only use a filter?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

You haven't really explained what you want to do with the session.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I want to store the session in a Map with session related data (such as Creation Time, IP, etc). So I am unsure about the place where I should implement the map to get the data.

When I use the listener, then I can store the HTTPSession but has no access to ServletRequest-Object.

So I assume, that I need only a filter, because a filter has access to the HTTPSession and the ServletRequest-Object. Am I right?

Or do I need both -filter and listener? The clickstream-API needs both, but is it not enough to have only the listener? I mean, when a session is created, then I can get the (requested?) SessionObject from my filter and not only from my listener. Where lies the difference?
[ October 16, 2008: Message edited by: nimo frey ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Why would you want to store a reference to the session? This is odd, uncustomary, and not very safe. You should never be storing references to container resources such as sessions, requests, responses and so on.

Again, what are you really trying to accomplish?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hmm..odd?

Look at the Clickstream-Api (http://www.opensymphony.com/clickstream/).

I want to build a session-tracking-tool and do not understand for what using a listener, when a filter have access to anything that the filter has.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I agree with Bear about storing sessions. If clickstream stores the session for what it does then it is poorly designed. It does not need to do it.

From the docs, the Filter is used to track a user's movement through the web site. It gets info for each request and stores it. Where it stores it is unimportant - possible in some user token inside the session or in a context-wide storage location mapped to a user.

The Session Listener is to track when a session ends. When the session ends (presumably user-log out) then the filter is used to send the tracked information into a log file. It could get that information out of the same storage that the filter uses - either from a token in the session, or from a context wide map.

In neither case do you need to store a reference to the session itself, and if clickstream is doing it then it shouldn't be looked at as normal procedure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: filter or listener