DWR (Direct Web Remoting) is an open-source library Combining Java and JavaScript. It can be used to easily construct Ajax programs. Developers do not need to understand the coding details of XMLHttpRequest. The client uses JavaScript to call the server code by calling the method that appears to call the local code of the browser. This is the meaning of "direct" in DWR. A notable feature of DWR is that it provides a server-side code as JavaScript code in a browser. In essence, DWR is an RPC (Remote Procedure Call, Remote process Call ). The basic concept of RPC can be traced back to 1976 (RFC707). In 1981, it was first used by Xerox for commercial applications. Currently, popular Java RMI, Microsoft. NET Remoting, Web Service, and earlier CORBA all follow the basic concepts of RPC established in 1976, while RPC is also the basis of Sun NFS. RPC is a mechanism for executing code in one address space and subprograms or processes in another address space or shared network. When implementing this mechanism, the caller programmer does not need to write code to implement remote interaction. The same Code is written both locally and remotely. Remote Call or remote method call have the same meaning as RPC. The former is called in an object-oriented environment.
Shows the architecture of RPC:
In RPC, the client sends a request using a proxy stub function or object through the underlying RPC library, and the called code is stored on another computer, requests are sent to remote computers through some network protocols. in the RPC library, the server component receives requests and uses the proxy stub object (this proxy stub is often omitted in implementation, its functions are merged by the server-side RPC component) and passed to the destination. After the service code is executed, the results are returned in the opposite process. For callers, remote code interfaces are equivalent to interfaces for codes run in the local process space. Remote interaction is transparent.
For details about the current mechanism of DWR, refer.
PopulateList () is a callback function that is passed as a parameter to getOptions (). When the remote call result of getOptions () is returned, this function is automatically called, to process the returned data. Obviously, the local getOptions () function has a callback function parameter compared to the getOptions () function on the server. With this parameter, asynchronous processing is implemented.
The main body of DWR on the server is the DWRServlet class, which automatically generates JavaScript Objects Based on Java classes. The specific generation process is:
If the HTML page of the Browser contains the following script:
<Script type = "text/javascript" src ="[WEBAPP]/Dwr/interface/AjaxService. js "> </script>
When the browser parses and executes the page, it sends a request like the corresponding URI. In the web. xml configuration of the server, the URI is mapped to DWRServlet.
------- Web. xml -------------
<Servlet>
<Servlet-name> dwr-invoker </servlet-name>
<Display-name> DWR Servlet </display-name>
<Servlet-class> org. directwebremoting. servlet. 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>
When DWRServlet receives the request, it will follow dwr. the ing relationships configured in xml are automatically checked by Java class your. java. ajaxService, and generate a JavaScript file representing this class, and return it to the browser. The JavaScript Object contained in this file is the client proxy stub.
------- Dwr. xml -------------
<Dwr>
<Allow>
<Create creator = "new" javascript = "JDate">
<Param name = "class" value = "java. util. Date"/>
</Create>
<Create creator = "new" javascript = "AjaxService">
<Param name = "class" value = "your. java. AjaxService"/>
</Create>
</Allow>
</Dwr>
In contrast to DWRServlet, the core of the client's DWR is engine. js, which embeds scripts in HTML pages.
<Script src = '/[YOUR-WEBAPP]/dwr/engine. js'> </script>
Downloaded by the browser to your local device. The JavaScript library contained in engine. js is used to assemble calls from dynamically generated proxy stubs into real objects on the server to complete remote communication and calling. Engine. js will output the DWREngine object, through which users can configure specific communication methods between the client and the server, such as synchronous or asynchronous, Http transmission, rpcType, timeout, and other parameters.
In addition, HTML pages are usually embedded with <script src = '/[YOUR-WEBAPP]/dwr/util. js'> </script>: util scripts are used as a tool set to assist in data processing, such as dynamic page refresh.
To sum up, the basic workflow of RPC can be described as follows: