How-To : Silverlight accessing Java REST services (Part 2 – No Cross Domain : Tomcat/Eclipse)
The HowTo serie demoes Silverlight accessing Web Services in various configuration. In this post, we will demonstrate a REST 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 XAP package, the REST service Eclipse project and the Silverlight Eclipse project are available as an attachment to this post.
Technical Environment
- REST services are built with the Restlet framework and hosted in Tomcat
- The Silverlight application is also hosted in Tomcat
Because the Site Of Origin and the Web Services reside in the same host, no Cross Domain policy is required.
To successfully install the technical environment, please take the following step :
- Build the scenario detailled in HowTo : Silverlight accessing Java REST services (Part 1 - Cross Domain : IIS + Tomcat)
If you don’t feel like doing the previous step, the XAP package available as an attachment contains a Silverlight client which is configured to access a REST service at https://localhost:8080/Restlet-servletfirstResource/items.
The REST service is also available as an attachment to this post allows to do a GET and POST method to get a list of items or to insert a new one.
Download the attachment and unzip it. There are two needed parts : the Silverlight XAP package and the Restlet Eclipse project.
Host the REST service with Tomcat
- Import the Reslet-servletfirstResource project into your Eclipse workspace :
- Run the project on Tomcat Server. check that https://localhost:8080/Restlet-servletfirstResource/items is accessible you should get the following result :
Host the Silverlight application in an Eclipse project running on Tomcat
- Create a new Dynamic Web Project
- In the REST-SilverlightClient project, create the WebContent\ClientBin folder and copy the XAP package into it.
- Now we need to host it through an Active X control :
- Right click on WebContent folder and select New—>JSP, call it index
- Edit index.jsp, in the Head part put the following script that catches Silverlight Error.
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError")
{
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError")
{
if (args.lineNumber != 0)
{
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
</script>
-
- In the Body part, instantiate a Silverlight control by adding the following HTML code, ensure that the param named “source” has is value set to the XAP package URI relative to index.jsp Web Page:
<div id="silverlightControlHost">
<object data="data:application/x-silverlight,"
type="application/x-silverlight-2-b2"
width="100%" height="100%">
<param name="source" value="ClientBin/REST-JavaServiceItem.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<a href="https://go.microsoft.com/fwlink/?LinkID=115261"
style="text-decoration: none;">
<img src="https://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>
- Run the project on Tomcat Server and the Silverlight control will appear in the Eclipse Web Browser :
The great thing about that is that you will be able to test your REST Service and the Silverlight application without running multiple Development Environments.
REST service + Silverlight Eclipse project + XAP - No Cross Domain.zip
Comments
Anonymous
August 05, 2008
PingBack from http://wordnew.acne-reveiw.info/?p=16445Anonymous
August 28, 2008
The HowTo series demo Silverlight accessing Web Services in various configuration. In this post, we willAnonymous
August 28, 2008
The HowTo series demo Silverlight accessing Web Services in various configuration. In this post, we will