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

getting the IP when the session is created

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hi ,
I'm using HttpSessionListener interface, to do some stuff as soon as the session is created , i need to get the client IP in this stage , is it applicable or i have to wait for any request coming from the client to get the IP ?


Thanks,
Sherif
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Session is created.. only when the request has come to Server..right ?


- Chinna
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Use "getRemoteAddr()" of ServletRequest interface.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

but there is not any request's object in the session listener ...
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Sherif Shehab wrote:but there is not any request's object in the session listener ...

Yes, HttpSessionListener is for get notified when a Session is created not for anything about a request. But you have access to the particular request object when creating a session right (that's when the listener get notified)?
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Vijitha Kumara wrote:
Sherif Shehab wrote:but there is not any request's object in the session listener ...

Yes, HttpSessionListener is for get notified when a Session is created not for anything about a request. But you have access to the particular request object when creating a session right (that's when the listener get notified)?


Vijitha , more clarification please
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

HttpSessionListener is passed HttpSessionEvent which you have access to only HttpSession through "getSession()" method. What exactly you want to do with the listener here ?
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Vijitha Kumara wrote:HttpSessionListener is passed HttpSessionEvent which you have access to only HttpSession through "getSession()" method. What exactly you want to do with the listener here ?

i need to get the Client IP when the session is created , this why i used the listener..
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

You cannot access ip in your HttpSessionListener, as you dont have request object.

But this HttpSessionListener is invoked, when you create session.
And the session is created using the request object.

Using that request object.. you can get the ip address of client.

If you still need any clarifications.. Post the code where you create the sessions.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Chinna Eranna wrote:You cannot access ip in your HttpSessionListener, as you dont have request object.

But this HttpSessionListener is invoked, when you create session.
And the session is created using the request object.

Using that request object.. you can get the ip address of client.

If you still need any clarifications.. Post the code where you create the sessions.


Yes Chinna this is my problem that i wanna get the Client IP when the as soon as the session is created , this why i used the HttpSessionListener to get the IP in sessionCreated(HttpSessionEvent se) method , but as you said i cant access the request object in the HttpSessionListener , so is there a way to get the IP in the HttpSessionListener , i know how to get the IP when from the request object , by using getRemoteAddr().
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

SessionListener is not the place for the creating a Session.

Sessions are created in either servlet/Action class (if you are using the Struts).

When you create a session.. the SessionListener is notified of that event.. that there is a session being created.

Tell me.. where are you creating the session.. either servlet/Action class or somewhere else ?
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Instead of doing this stuff use filters where you will get Request & Response Object & you can track the IP Address,Name of Incoming Request.

I hope this will help you.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

swapnl patil wrote:Instead of doing this stuff use filters where you will get Request & Response Object & you can track the IP Address,Name of Incoming Request.

I hope this will help you.

I know you are right but , i need to get the IP as soon as the session was created, so this needs session listener.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
i need to get the IP as soon as the session was created, so this needs session listener.

How is the session created? If it's in code under your control, can't you store the IP address as a session attribute right after creating it?


Ping & DNS - my free Android networking tools app
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Guys ,

Now i;m little bit confused about when this method is called public void sessionCreated(HttpSessionEvent se) , is it called when the server creates the session for the first time or when i create a session object like this : in any place in my application ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Sherif Shehab wrote:Now i;m little bit confused about when this method is called public void sessionCreated(HttpSessionEvent se) , is it called when the server creates the session for the first time or when i create a session object like this : in any place in my application ?

When that code is run as a result of a request by a particular client for the fiirst time, container creates a session object and returns it. So the "sessionCreated" of the listener also called. You can check whether a new session created or not by "session.isNew()". If a session associated with that client is found the that code returns the existing one.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hmmm ,i understood now .
Thanks Guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the IP when the session is created