Java Reference
In-Depth Information
cess to the headers and data in the request, and can use them to base your response on as ne-
cessary.
For the response, you set the content type to “text/xml” for a SOAP message. If you kept
the default of “text/html”, the browser would attempt to interpret the response as HTML. For
this example, that would mean that it would show only your greeting in the browser because
it would suppress the tags it doesn't understand (which in the case of SOAP is all of them).
By changing the content type to “text/xml”, the entire message will show in the browser. Of
course, in the real world, you probably wouldn't have a use case for doing that, and you might
instead be calling this servlet in another manner and be prepared to handle a SOAP response.
Finally, the SOAPMessage.writeTo method is used to send the constructed response to the
output stream retrieved from the HttpServletResponse . That sends the response containing
the SOAP message and all of your regular HTTP header information back to the client.
To invoke your web service, you'll cheat a little bit. That is, you won't send it a SOAP request,
but rather create a quick JSP that invokes the servlet with a link. The link will pass the servlet
a person's name as a request parameter, which you'll use to return a SOAP response contain-
ing a classic greeting.
So your JSP looks like this:
<html>
<body>
<a href="SAAJProviderServlet?name=Eben">CLICK ME</a>
</body>
</html>
There's no real SOAP client and no handling of a SOAP response here, just a link that calls
the servlet. This chapter provides many examples of getting a SOAP response, so we'll just
stick to the server here.
The servlet is deployed with a mapping in the web.xmlthat is easily invoked relative to the
JSP (they're in the same WAR in this example):
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>SAAJProviderServlet</servlet-name>
<servlet-class>saaj.provider.SAAJProviderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SAAJProviderServlet</servlet-name>
Search WWH ::




Custom Search