Version 2

    Web component clients

     

    The service client programming model for web components is about how to access a remote web service from any kind of web component (i.e. servlets, jsp pages). The web service client is deployed as a standard war archive plus additional <service-ref> elements in the web.xml deployment descriptor.

     

    Service Endpoint Interface

     

    The SEI declares the methods through which to access the remote web service.

    We use the same service endpoint interface as for the EJB service endpoint.

     

    Web Service Description Language

     

    Any web service client must have access to the wsdl that describes the remote webservice. The wsdl can be part of

    the web deployment or may be obtained at runtime before the servlet accesses the remote web service.

     

    JAX-RPC Mapping Files

     

    The web service client must have access to the same mapping information as described for the

    EJB service endpoint or the Java service endpoint. The jaxrpc mapping file must be

    part of the deployment, it cannot be obtained at runtime.

     

    Packaging the web service client

     

    The standard deployment descriptor for web components web.xml, declares the service reference.

     

      <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      
        <servlet>
          <servlet-name>OrganizationClientServlet</servlet-name>
          <servlet-class>com.underworld.crimeportal.web.OrganizationClientServlet</servlet-class>
        </servlet>
      
        <servlet-mapping>
          <servlet-name>OrganizationClientServlet</servlet-name>
          <url-pattern>/*</url-pattern>
        </servlet-mapping>
      
        <service-ref>
          <service-ref-name>service/OrganizationServiceJSE</service-ref-name>
          <service-interface>com.underworld.crimeportal.OrganizationEndpointService</service-interface>
          <wsdl-file>WEB-INF/wsdl/server-web.wsdl</wsdl-file>
          <jaxrpc-mapping-file>WEB-INF/jaxrpc-mapping.xml</jaxrpc-mapping-file>
        </service-ref>
      
      </web-app>
    

     

    Service Interface

     

    It is possible to provide a typed service interface to web service clients that extends the standard

    javax.xml.rpc.Service interface. In this way the client looks up a service from JNDI that already

    has application specific semantics.

     

       public interface OrganizationEndpointService extends javax.xml.rpc.Service
       {
          public OrganizationEndpoint getOrganizationPort() throws javax.xml.rpc.ServiceException;
       }
    

     

    Deploy the Web Service client to JBoss

     

    This follows the normal deployment procedure for web components in JBoss. There are no web service specific info

    messages.

     

    Accessing the remote web service

     

    Here is a simple example, that shows how a servlet does a lookup of the service interface, then obtains the

    desired port, and finally invokes getContactInfo on the service endpoint interface.

     

    public class OrganizationClientServlet extends HttpServlet
       {
          // provide logging
          private static final Logger log = Logger.getLogger(OrganizationClientServlet.class);
       
          protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
          {
       
             String organization = req.getParameter("organization");
             String info = getContactInfoJSE(organization);
       
             PrintWriter out = res.getWriter();
             out.print(info);
             out.close();
          }
       
          /** Get the contact info from the JSE service */
          public String getContactInfoJSE(String organization) throws ServletException
          {
             try
             {
                InitialContext iniCtx = new InitialContext();
                OrganizationEndpointService service = (OrganizationEndpointService)iniCtx.lookup("java:comp/env/service/OrganizationServiceJSE");
                OrganizationEndpoint endpoint = service.getOrganizationPort();
                String info = endpoint.getContactInfo(organization);
                return info;
             }
             catch (NamingException e)
             {
                throw new ServletException(e);
             }
             catch (Exception e)
             {
                throw new ServletException("Cannot invoke webservice", e);
             }
          }
       }