Skip to main content

Maintaining Session in JAX-WS

1 reply [Last post]
kumar9g
Offline
Joined: 2010-07-28
Points: 0

We used jax-ws as WebService interface for our server-application. I was trying to maintain session using jax-ws. i used several links including Rama Pulavarthi's example.

package session.client;

import session.server.*;
import javax.xml.ws.BindingProvider;

public class HelloClient {
public static void main(String[] args) {

Hello proxy = new HelloService().getHelloPort();
((BindingProvider)proxy).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY,true);
int result = proxy.getCounter();
System.out.println(result);
result = proxy.getCounter();
System.out.println(result);

}
}

Simulated above code inside a jsp. This prints result as 0,1 and 2 respectively when jsp gets loaded(using out.println(result) 3 times in my code). But using a button click to again call the same service to get counter prints result as 0,1 and 2 again instead of 3,4 and 5 (session-id gets changed which means it lost its previous session parameters).

Can anyone please help me regarding this ? What can be done to maitain session. I need to track user-session for login and logout timestamp and amount of time user holds session.

Need help on this at your earliest convinience. Any suggestions are welcome.
Thank you.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chessc
Offline
Joined: 2010-08-23
Points: 0

>I need to track
> user-session for login and logout timestamp and
> amount of time user holds session.
>
> Need help on this at your earliest convinience. Any
> suggestions are welcome.
> Thank you.

Hello,

It seem that the code you show is for client side. If you want to track session, it's better to do at the server side.

For that, I use a session listener (HttpSessionListener class) that provide sessionCreated and sessionDestroyed. You also need to declare you listener in web.xml.

I you use Netbeans, it's easy.
In you existing web application, create a new class, (eg : MyListener) and add implentation for HttpsessionListener. The result may look like :
public class MyListener implements HttpSessionListener

Next, double click on your web.xml file. Focus on "General" tab, section "Web Application Listeners". They must exist one (com.sun.xml....). So add AFTER your custom listener (with package name) : mypackage.MyListener.

I think you also need data session and also global data (across session).

For user session, it's look like :
MessageContext mc = context.getMessageContext();
HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();

I typically use this code after a successful auth and use to store username, ...

and set/get need attribute (ex : user name, ...) in session.

For global, in servletcontext, it's look like :
MessageContext mc = context.getMessageContext();
ServletContext sc = (ServletContext)mc.get (MessageContext.SERVLET_CONTEXT);

and set/get attribute in session.

I use servlet level attribute for track session count, or other global stuff.

Hope this help you,
Regards,