The HowTo series demo Silverlight accessing Web Services in various configuration. In this post, we will demonstrate you a SOAP service consumed by a Silverlight client application where the Site of Origin hosts the Web Services. This scenario is categorized as a D2D scenario without cross-domain policy.

The SOAP service Eclipse project and the Silverlight Eclipse project are available as an attachment to this post.

Technical Environment

  • SOAP services are built with the JAX-WS and JAX-B Api and hosted in Tomcat
  • The Silverlight application is also hosted in Tomcat

image

Because the Site of Origin and the Web Service reside in the same host, no Cross Domain policy is required.

Hosting the Web Service in Tomcat

In case you haven’t done the How-To : Silverlight accessing Java SOAP services (Part 1 – Cross Domain : IIS + Tomcat), you can directly import the SOAP-Sum service Eclipse project into your Eclipse Workspace.

image

Hosting the Silverlight Application using a Servlet

In the REST way, we showed you how to host a Silverlight Application using a JSP. Now we are going to host a Silverlight application using a Servlet

  • In Eclipse, create a new dynamic web project named Silverlight-SOAP-SumService_NOSSL
  • In WebContent folder, add a folder named ClientBin and copy in the latter, the xap file in the attachment. The Silverlight Application is already configured to access the SOAP service at http://localhost:8080/SOAP-SumService/services/sum?wsdl.
  • Add a Servlet named SilverlightServlet to a package ‘servlet’ and add the following code :

Instance of Silverlight plugin :

private void responseHandler(HttpServletResponse response) throws IOException{
    PrintWriter pw = response.getWriter();
    pw.println("" +
            "<div id=\"silverlightControlHost\">" +
            "    <object data=\"data:application/x-silverlight,\"" +
            "             type=\"application/x-silverlight-2-b2\"" +
            "             width=\"100%\" height=\"100%\">" +
            "       <param name=\"source\" value=\"ClientBin/SOAP-Sum.xap\"/>" +
            "       <param name=\"background\" value=\"white\" />" +
            "       <a href=\"http://go.microsoft.com/fwlink/?LinkID=115261\"" +
            "          style=\'text-decoration: none;\'>' +
            "         <img src=\"http://go.microsoft.com/fwlink/?LinkId=108181\""+
            "              alt=\"Get Microsoft Silverlight\"" +
            "              style=\'border-style: none\'/>' +
            "        </a>" +
            "    </object>" +
            "    <iframe style='visibility:hidden;height:0;width:0;border:0px'>' +
            "</iframe>" +
            "</div>");
    pw.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    responseHandler(response);
    super.doGet(request, response);
}

Silverlight will access the SOAP-Sum service without cross-domain

image

- Ronny Kwon