Java Reference
In-Depth Information
you need to configure the client to maintain the session across requests. Example 7-17 shows
the client that will invoke the service and get a counter incremented.
Example7-17.Stateful web service client JSP
<%@page contentType="text/html" pageEncoding="UTF-8"
import="com.soacookbook.*, javax.xml.ws.*"%>
<html>
<head><title>Counter Service</title></head>
<body>
<h1>Counter Context Service</h1>
<hr/>
<%
try {
CounterContextWS port = null;
if (session.isNew()){
CounterContextWSService service = new
CounterContextWSService();
port = service.getCounterContextWSPort();
session.setAttribute("port", port);
((BindingProvider)port).getRequestContext().put(
BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
}
port = (CounterContextWS)session.getAttribute("port");
int result = port.getCounter();
out.print(result);
result = port.getCounter();
out.print("<br/>");
out.print(result);
} catch (Exception ex) {
ex.printStackTrace();
}
%>
</body>
</html>
Let's unpack this client code a bit. The BindingProvider class is in the javax.xml.ws pack-
age, so you import that at the top, along with the generated JAX-WS artifacts for your service.
Then check if the session is new, and if so, create a new stub and associate it with this ses-
sion by setting it as an attribute. You now get the request context, and use the BindingPro-
vider class to indicate that you want to maintain session state across requests for this service.
Search WWH ::




Custom Search