Intergrate DWR into Your Java Web Application - Direct Web Remoting (DWR) for Java Jocks
(Page 3 of 4 )
You may have to restart the Java web application for the servlet container to create a new instance of this DWR-related servlet.
You also have to create a simple XML file declaring the Java classes that you want to use from your client-side JavaScript code. Don’t worry, I’ll show you how to use the JavaScript objects that are bound to Java classes shortly! The file is named dwr.xml. Place this XML file in /WEB-INF/:
<dwr>
<allow>
<create creator="new" javascript="JsDate">
<param name="class" value="java.util.Date"/>
</create>
<create creator="new" javascript="JsBikeBean">
<param name="class" value="com.parkerriver.BikeBean"/>
</create>
</allow>
</dwr>
This XML states that the client-side JavaScript can use two Java classes remotely. The JavaScript objects that bind the client-side code remotely to the Java classes are namedJsDateandJsBikeBean. As part of the server-side preparations, you must have already developed the Java class
com.parkerriver.BikeBeanand installed it in your application.java.util.Dateis part of the Java software development kit; it’s not your own custom class.Date is already available as part of the Java virtual machine your server component is using.
TheBikeBeanclass file is typically stored in /WEB-INF/classes, as in /WEB-INF/classes/com/parkerriver/BikeBean.class.
This XML file binds the two JavaScript names to theDateandBikeBeanobjects, so that these objects are available to use in your client-side JavaScript. This means that JavaScript code can call all the public methods of these Java objects. But how is the JavaScript in the local web page connected to the remote Java instances running on the server? Figure 5-1 shows in general terms the path a JavaScript method call takes in DWR’s form of web remoting.
The web page that will use DWR contains thesescripttags, which connect the JavaScript code via the DWR servlet to the server code:
<script type="text/javascript" src=
"/[name of web app]/dwr/interface/JsBean.js">
</script>
<script type="text/javascript" src=
"/[name of web app]/dwr/interface/JsDate.js">
</script>
Next: Direct Web Remoting (DWR) for Java Jocks continued >>
More JavaScript Articles
More By O'Reilly Media
|
This article is excerpted from Hack 42 of the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). Check it out today at your favorite bookstore. Buy this book now.
|
|