Getting Started with WebSphere Application Server
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
138 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> Community Edition<br />
Figure 7.8 – Response after calling the Web service from a servlet<br />
7.3.4 Using service reference tag in a Java EE application<br />
In the previous servlet, you accessed the Web Service by instantiating the Service class<br />
manually. This method is not optimal because each time you process the servlet, you have<br />
to create a new service instance. A better alternative is to use JNDI to directly access your<br />
Web service <strong>with</strong>out instantiating any Service class. This method requires you to add a<br />
service-ref element to web.xml to provide the necessary information about the WSDL<br />
file location, Service name, etc. Listing 7.8 provides and example of what you need to add<br />
to web.xml.<br />
<br />
services/Hello<br />
<br />
com.ibm.wasce.samples.HelloBillService<br />
<br />
<br />
http://localhost:8080/HelloServiceProject/Hello?wsdl<br />
<br />
<br />
Listing 7.8 – The service reference tag<br />
Let’s go through the elements that were added to web.xml:<br />
1. service-ref-name - This is the name by which JNDI identifies your deployed<br />
service. These services are located at java:comp/env/<br />
2. service-interface - This is the interface which a client uses to access the Web<br />
service and create necessary stubs under the covers. This interface must extend<br />
either to javax.xml.ws.Service or javax.xml.rpc.Service. Note that this is not the<br />
Service-Endpoint Interface that we used to deploy the service.<br />
3. wsdl-file - The location of the WSDL file of the deployed web service.<br />
The changes required to access the service are shown in Listing 7.9<br />
protected void doPost(HttpServletRequest request, HttpServletResponse<br />
response) throws ServletException, IOException {<br />
PrintWriter out = response.getWriter();<br />
String target = request.getParameter("target");<br />
try {<br />
Context ctx = new InitialContext();<br />
HelloBillService billService =<br />
(HelloBillService)ctx.lookup("java:comp/env/services/Hello");<br />
HelloPeople bill = billService.getHelloBillPort();<br />
out.println(bill.sayHello(target));<br />
} catch (NamingException e) {