1, DWR: Easy AJAX for JAVA

As a java open source library, DWR can help developers to complete the application of AJAX technology, web procedures. It allows the browser on the javascript method call in web server running on Ways java.

DWR consists of two departments. javascript and web server and update web pages; run Servlet in the web server handle the request and respond to put hair back to the browser.

DWR to adopt innovative approaches to implementation of AJAX (originally there is no precise definition), in the java code based on the dynamic generation of javascript code. web developers to directly call these javascript code, but really the code is running on web server on the java code. With security considerations, the developer must configure the java class which exposed to the DWR. (Dwr.xml)

This from the (java to javascript) call mechanisms give the user a feeling, as if conventional RPC mechanism, or RMI or SOAP. But it is running in web on, without any browser plug-in.

DWR do not think that browser and web server protocol between the essential, first put the system interface. The greatest challenge is the synchronization java method call asynchronous ajax features and characteristics of the conflict between the. In the asynchronous model, the results only at the end of Ways before it becomes binding. DWR to resolve this problem, the callback function as a parameter to the method of treatment is completed, automatically call the callback method.

This chart shows, through the javascript case, DWR can change the content of select course content back from java code. javascript function Data.getOptions (populateList) dynamically generated by DWR, this function will call the java class Data type approach. DWR how to deal with long-distance calls, including the conversion of all the parameters and return the results (javascript \ java). Ways java implementation after the implementation of callback methods populateList. Throughout the process, we wanted to use local methods at the same.

2, Getting Started

Talk nonsense, to try out on the ok.
web.xml

<? xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / DTD Web Application 2.3 / / EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name> dwr-invoker </ servlet-name>
<servlet-class> uk.ltd.getahead.dwr.DWRServlet </ servlet-class>
</ servlet>
<servlet-mapping>
<servlet-name> dwr-invoker </ servlet-name>
<url-pattern> / dwr / * </ url-pattern>
</ servlet-mapping>
</ web-app>

dwr.xml the same directory and web.xml
<? xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE dwr PUBLIC "- / / GetAhead Limited / / DTD Direct Web Remoting 1.0 / / EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</ create>
</ allow>
</ dwr>

index.html
<html>
<head>
<title> DWR - Test Home </ title>
<script type='text/javascript' src='dwr/interface/JDate.js'> </ script>
<script type='text/javascript' src='dwr/engine.js'> </ script>
<script>
function init () (
JDate.getYear (load);
)
function load (data) (
alert (data +1900 + 'years')
)
</ script>
</ head>
<body onload="init()">
</ body>
</ html>

lib under dwr.jar download release

Over, what, enough of these. Visit ok!
3, Examples
http://www.aboutmyhealth.org/ This is not Google Suggest吗! ok.
4, source code Analysis
DWR Design webwork2 very much like the design, hidden http protocol, the scalability, compatibility and strong.

Through research uk.ltd.getahead.dwr.DWRServlet the DWR servlet to study under in the end is how to drop job.

Java code

1. Web.xml configuration
2. <servlet>
3. <servlet-name> Dwr-invoker </ servlet-name>
4. <servlet-class> Uk.ltd.getahead.dwr.DWRServlet </ servlet-class>
5. </ Servlet>
6. <servlet-mapping>
7. <servlet-name> Dwr-invoker </ servlet-name>
8. <url-pattern> / Dwr / * </ url-pattern>
9. </ Servlet-mapping>
10. So that all of the / dwr / * all requests by the servlet to handle, deal with it in the end what can be. We also described in the above example of the simplest.
11. 1, web server starts, DWRServlet init () method call, init major job to do the following.
12. Set the log level, examples of DWR Example of single-use category (these categories at JVM only one instance of the object), go read configuration file (including dwr.jar package dwr.xml, WEB-INF/dwr.xml . config *. xml).
13.2, request treatment
14. DWRServlet.doGet, doPost method call processor.handle (req, resp) method of treatment. Processor object at init () method has been initialized.
15. <pre Name="code"> public void handle (HttpServletRequest req, HttpServletResponse resp);
16. Throws IOException
17. (
18. String pathinfo = req.getPathInfo ();;
19. If (pathinfo == null | | pathinfo.length (); == 0 | | pathinfo.equals ("/"););
20. (
21. Resp.sendRedirect (req.getContextPath (); + req.getServletPath (); + '/' + "index.html");;
22.) Else
23. If (pathinfo! = Null & & pathinfo.equalsIgnoreCase ( "/ index.html "););
24. (
25. DoIndex (req, resp);;
26.) Else
27. If (pathinfo! = Null & & pathinfo.startsWith ( "/ test /"););
28. (
29. DoTest (req, resp);;
30.) Else
31. If (pathinfo! = Null & & pathinfo.equalsIgnoreCase ( "/ engine.js "););
32. (
33. DoFile (resp, "engine.js", "text / javascript");;
34.) Else
35. If (pathinfo! = Null & & pathinfo.equalsIgnoreCase ( "/ util.js "););
36. (
37. DoFile (resp, "util.js", "text / javascript");;
38.) Else
39. If (pathinfo! = Null & & pathinfo.equalsIgnoreCase ( "/ deprecated.js "););
40. (
41. DoFile (resp, "deprecated.js", "text / javascript");;
42.) Else
43. If (pathinfo! = Null & & pathinfo.startsWith ( "/ interface /"););
44. (
45. DoInterface (req, resp);;
46.) Else
47. If (pathinfo! = Null & & pathinfo.startsWith ( "/ exec "););
48. (
49. DoExec (req, resp);;
50.) Else
51. (
52. Log.warn ( "Page not found. In debug / test mode try viewing / [WEB-APP] / dwr /");;
53. Resp.sendError (404);;
54.)
55.) </ Pre>
56. <br> Oh. These came. dwr / * deal with these types of requests it.
57. <br> (1) dwr / index.html, dwr / test / This can only be used in debug mode, debugging use.
58. <br> Dwr / engine.js, dwr / util.js, dwr / deprecated.js When the request arrival, dwr.jar packet read from the document flow, in response to go back. (There is duplicate request cache)
59. <br> (2) When the dwr / interface / the arrival of such a request, (such as index.html in at our <script type='text/javascript' src='dwr/interface/JDate.js'> </ script>) DWR to do a great thing. WEB-INF/dwr.xml put us at the
60. <br> <create Creator="new" javascript="JDate">
61. <br> <param Name="class" value="java.util.Date"/>
62. <br> </ Create>
63. <br> Java.util.Date into a javascript function.
64. <br> Http://localhost:port/simpledwr/dwr/interface/JDate.js look at it.
65. <br> Details are also relatively simple, through java reflection methods are put into a particular method of javascript. (I think these can be converted into the cache, the next call is no need to re-generate it again, I do not know why the author did not do so).
66. <br> (3) dwr / exec
67. <br> Javascript method call to send such a request may be sent XMLHttpRequest or IFrame.
68. <br> Of course, javascript method call Signature with java code, including the parameters have javascript callback method can also spread to a server-side, in the server-side implementation is easy. Callback method java implementation of the results of a similar return <script> callMethod (results) <script> string of javascript in the browser implementation. Ha, all that simple, ingenious.
69. <br>
70. <br> Dwr design ideas are very clever.
71. <br> First, put java category into categories by the DWR javascript auto-complete, a simple configuration.
72. <br> Second, the application be extremely easy. Developers not to code on the server can be integrated.
73. <br> Thirdly, the easier test. And WebWork as hidden http protocol.
74. <br> Fourth, and strong scalability. Such as integration with the spring, just modify a little code.
75. <br> Fifth, performance. On me and jason, and so relatively easy, dwr performance might be the best.
76. <br> Sixth, automatically put into a javascript object java object, and and easy expansion.