DWR introduction
Thursday, December 30, 2010
What is DWR?
• Is a Java and JavaScript open source library which
allows you to write Ajax web applications
> Hides low-level XMLHttpRequest handling
• Specifically designed with Java technology in mind
> “Easy AJAX for Java”
• Allows JavaScript code in a browser to use Java
methods running on a web server just as if they
were in the browser
> Why it is called “Direct remoting”
Why DWR?
• Without DWR, you would have to create many
Web application endpoints (servlets) that need to
be address'able via URI's
• What happens if you have several methods in a
class on the server that you want to invoke from
the browser?
> Each of these methods need to be addressable via URI
whether you are using XMLHttpRequest directory or clientside
only toolkit such as Dojo or Prototype
> You would have to map parameters and return values to
HTML input form parameters and responses yourself
• DWR comes with some JavaScript utility functions
based on my example what i observed is using dwr we can convert java class to javascript class. using this javascript class we can call java class methods in client side itself.
Creating JavaClass:
Configuring DWR servlet in web.xml
web.xml
creating dwr.xml to convert java class to javascript class:
dwr.xml
here we are converting java class to javascript class using creator.so we can call javaclass methods using this javascript class.
Creating jsp page for frontend:
you can read above comments to understand this concept.we have to include engine.js and util.js files and we have to include our generated javascript class using dwr.xml.that is present in dwr/interface folder.
now execute this example and you will get message from server. if you have any doubts post your comments
Download:
Download DWR example with source
To execute open with netbeans and run index.jsp
• Is a Java and JavaScript open source library which
allows you to write Ajax web applications
> Hides low-level XMLHttpRequest handling
• Specifically designed with Java technology in mind
> “Easy AJAX for Java”
• Allows JavaScript code in a browser to use Java
methods running on a web server just as if they
were in the browser
> Why it is called “Direct remoting”
Why DWR?
• Without DWR, you would have to create many
Web application endpoints (servlets) that need to
be address'able via URI's
• What happens if you have several methods in a
class on the server that you want to invoke from
the browser?
> Each of these methods need to be addressable via URI
whether you are using XMLHttpRequest directory or clientside
only toolkit such as Dojo or Prototype
> You would have to map parameters and return values to
HTML input form parameters and responses yourself
• DWR comes with some JavaScript utility functions
- create a web application using netbeans
- download dwr.jar files and include them in your web-inf/lib folder. you can find these jar files in my example
- edit web.xml to include dwr servlet
- create dwr.xml file to convert java class to javascript class
- create jsp page
- create java class
- call java class method using converted javascript class in jsp page
- now execute this applicaton
based on my example what i observed is using dwr we can convert java class to javascript class. using this javascript class we can call java class methods in client side itself.
Creating JavaClass:
package mypack; public class MyJavaClass { public String getMessage() { return "Wow! First DWr Example Success.hello from " + "Jagadeesh using server" ; } //we will call this method //in client side using javascript class }
Configuring DWR servlet in web.xml
web.xml
<web-app> <servlet> <servlet-name> dwr-invoker </servlet-name> <servlet-class> uk.ltd.getahead.dwr.DWRServlet </servlet-class> <init-param> <param-name> debug </param-name> <param-value> true </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> dwr-invoker </servlet-name> <url-pattern> /dwr/* </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>here we are giving url path to dwr servlet. if you exctract dwr.jar files you can find that class in uk.ltd.getahead.dwr folder. if i learn more about dwr i will explain about this in later examples.if you know more about this xml file please post your comment below then other people can learn that
creating dwr.xml to convert java class to javascript class:
dwr.xml
<dwr> <allow> <create creator="new" javascript="MyJavaScriptClass"> <!-- here MyJavaScriptClass is dynamic javascript class generated by dwr--> <param name="class" value="mypack.MyJavaClass"/> <!-- now dwrservlet convert mypack.MyJavaClass to MyJavaScriptClass. so we can call methods of mypack.MyJavaClass using MyJavaScritpClass--> </create> </allow> </dwr>
here we are converting java class to javascript class using creator.so we can call javaclass methods using this javascript class.
Creating jsp page for frontend:
<%-- Document : index Created on : Dec 30, 2010, 9:37:30 PM Author : Jagadeesh --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Dwr Example</title> <!-- You have to include these two JavaScript files from DWR --> <script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/util.js'></script> <!-- This JavaScript file is generated specifically for your application --> <!-- in dwr.xml we have converted MyJavaClass to MyJavaScriptClass.js--> <!-- so we can call java class methods using this javascript class--> <script type='text/javascript' src='dwr/interface/MyJavaScriptClass.js'> </script> <script> function getMessageFromServer() { //getMessage is a method in java class. we can call this method //using our generated javascript class //handleReceivedData is a call back method MyJavaScriptClass.getMessage(handleReceivedData); } function handleReceivedData(str) { alert(str); } </script> </head> <body onload="getMessageFromServer()"> </body> </html>
you can read above comments to understand this concept.we have to include engine.js and util.js files and we have to include our generated javascript class using dwr.xml.that is present in dwr/interface folder.
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='dwr/interface/MyJavaScriptClass.js'> </script>
now execute this example and you will get message from server. if you have any doubts post your comments
Download:
Download DWR example with source
To execute open with netbeans and run index.jsp
Nice and Simple example of DWR......
Thanks. for latest updates visit my new blog
www.myrmidons.co.in
i will post springs,hibernate video tutorials soon
Nice Post :
My Post on DWR with JSP