Show TOC

Creating the Web Service Client ApplicationLocate this document in the navigation structure

Use

You create a Web service client application to consume a Web service. The Web service client application connects to the Web service and consumes it via a Web service proxy.

In this tutorial, you use a Java Web servlet as a client application.

Procedure
  1. In the Java EE perspective, choose the HelloWorldWEB project, and then choose Start of the navigation path File Next navigation step New Next navigation step Other End of the navigation path.

  2. In the list of wizards, choose Start of the navigation path Web Next navigation step Servlet End of the navigation path, and then choose Next .

  3. In the Javapackage field, enter com.sap.tutorial.servlet .

  4. In the Classname field, enter HelloServlet , and then choose Finish .

    The system opens the created servlet for editing.

  5. Update the source code as shown in the example below.

    The @WebServiceRef annotation declares a reference to the Hello S ervice Web service.

                      package com.sap.tutorial.servlet;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.ws.WebServiceRef;
    
    import com.sap.tutorial.helloworld.HelloRemote;
    import com.sap.tutorial.helloworld.HelloService;
    /**
     * Servlet implementation class for Servlet: HelloServlet
     *
     */
     public class HelloServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
        
             @WebServiceRef(name = "HelloService")
             HelloService service;
             public HelloServlet() {
                    super();
            }       
            
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    // TODO Auto-generated method stub
                    response.setContentType("text/html");
                    PrintWriter out = response.getWriter();
          out.println("<?xml version=\"1.0\"?>");
          out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
          out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
          out.println("<head><title>Hello World Web service</title></head>\n");
          out.println("<body>");
          out.print("<form action=\"");
          out.print("HelloServlet\" ");
          out.println("method=get>");
          String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        HelloRemote emptyHelloBeanPort = service.getHelloBeanPort();
        String name = firstname+" "+lastname;
         if (firstname != null || lastname != null) {
           String result = emptyHelloBeanPort.sayHello(new String(new String(name)));
           out.println("<p><strong>"+result+"</strong></p>");
          } else {
              out.println("<strong>"+"Enter first and last name"+"</strong>");
          }
          out.println("<table style=\"margin: 10px 0px;\" summary=\"Enter names\">");
          out.println("<tr>");
          out.println("<td>First name</td>");
          out.println("<td><input type=text size=20 name=firstname /></td>");
          out.println("</tr>");
          out.println("<tr>");
          out.println("<td>Last name</td>");
          out.println("<td><input type=text size=20 name=lastname /></td>");
          out.println("</tr>");
          out.println("</table>");
          out.println("<input type=submit value=Execute />");
          out.println("</form>");
          out.println("</body>");
          out.println("</html>");
            }
    }
    
                   
  6. Save your changes.