• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting the IP when the session is created

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session is created.. only when the request has come to Server..right ?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "getRemoteAddr()" of ServletRequest interface.
 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but there is not any request's object in the session listener ...
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm ,i understood now .
Thanks Guys
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic