Extend your code’s reach by calling built-in Java objects remotely.
What if you had to read a file like a log on a server from a JavaScript object on the client browser? You might want to use the java.io.FileReader class on the server. (This class is part of the Java 2 Standard Editiona fancy way of saying that FileReader is built into Java but not JavaScript.) The DWR framework allows you to easily call standard Java methods from your JavaScript. This hack displays some date information on a web page. The data derives from remote method calls using the java.util.Date object.
JavaScript has a robust Date object and several associated methods, which you would use in most real-world applications that display dates on a web page. It’s still nice to know, from at least a hack writer’s perspective, that a great variety of standard Java objects and their methods are available from JavaScript. At the very least, you can adapt these techniques to several other similar situations.
The code in this hack displays the current date and time, and compares this data to the Greenwich mean time (GMT) date and time.
Setting up this code involves a little server configuration, as this chapter’s first hack explained. (If you’re still setting up DWR on the server, check back to “Integrate DWR into Your Java Web Application” for a summary of the required steps.) Here is the configuration file to place on the server:
<dwr>
<allow>
<create creator="new" javascript="JsDate">
<param name="class" value="java.util.Date"/>
</create>
</allow>
</dwr>