How to make JAX-WS webservice respond with JSESSIONID (session id)

nisenish picture nisenish · Jan 13, 2012 · Viewed 10k times · Source

I am a newbie in web services. I have a JAX-WS service which I need to implement session mechanism for. The SOAP messages are transported over HTTP, we use WebLogic, so JAXWS application is deployed on WebLogic app server and the services are accessible from WSDL document.

I have @WebServiceProvider (class that implements Provider< SOAPMessage >)

Now when I fire a login request I want JSESSIONID session cookie to be sent back, but we don't want to use CXF or anything else, just so called Metro, which frankly I don't yet completely understand. We also do not want to make this a persistent cookie, so manually adding a cookie to the response header is also not an option. But that works, I tried it. I just do not understand why session cookie is not set up automatically.

I've been searching the web and trying many things for 4 days now, nothing works. Please help.

Answer

Kal picture Kal · Jan 13, 2012

Typically, just accessing the HttpSession in your web service should be sufficient to set the session cookie in your response.

You can do this by injecting the WebServiceContext into your web service like so --

@Resource
private WebServiceContext ctx;
public void webServiceMethod() {
     MessageContext mc = ctx.getMessageContext();
     HttpSession session =    ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
     if (session == null)
         throw new WebServiceException("No HTTP Session found");