You want your web service to keep track of client sessions, much as you could do with a servlet.
This is a tricky one—at least as of this writing. There are a few different solutions, none of them is optimal. Here are your options:
If you are using HTTP as the transport layer, declare a
WebServiceContext object as a
Resource, and use it to get the
HttpSession object. Use the
HttpSession as you normally would in a
servlet. The only downside is that it does not work for transports
other than HTTP. But it’s straightforward and portable.
If you are using the JAX-WS reference implementation, you can
use a simple annotation,
This is a nice solution that wraps the first solution just
mentioned, so there is a little less coding. There is no need to do
anything but store your desired state in regular instance variables,
and the runtime will hand out one service instance per HTTP session.
However, it is specific to the RI and it does not work just yet.
Finally, you can use WS-Addressing, which works outside of HTTP, but this is very complex. WS-Addressing is outlined in Chapter 12.
HTTP is a stateless protocol, meaning that each new connection bears no relation to any previous connection; state is not held between requests. While this is a fundamental feature of HTTP, there are many times when keeping state (particular knowledge of each request as it relates to ...