views:

39

answers:

1

How to get session object while working on webservices?

Services are called between two programs. How to get user session object while workin with webservices. It is not possible to get session using request object as there will not be request or response when we talk about services.

A: 

If you're working with JAX-WS to create your web services, then you can access the HttpServletRequest object (and hence your HttpSession object) via the WebServiceContext.

@WebService(...)
public class MyService {
    @Resource
    private WebServiceContext ctx;

    private HttpSession getSession() {
        HttpServletRequest req = (HttpServletRequest) this.ctx.getMessageContext()
                .get(MessageContext.SERVLET_REQUEST);
        return req.getSession();
    }
}

For a more extensive example, see, e.g., "Maintaining sessions using JAX-WS 2.0" by Art Frechette.

janko