How DWR implements Ajax
First, the introduction of Ajax.
Ajax = asynchronous JavaScript and XML (asynchronous JavaScript and XML).
AJAX is not a new programming language, but a new way to use existing standards.
The biggest advantage of AJAX is that it can exchange data with the server and update some web pages without reloading the whole page.
AJAX doesn't need any browser plug-ins, but it needs users to allow JavaScript to execute on the browser.
Second, the introduction of DWR
1. official website address. Smileyan.cy.Cyservice "This can also be modified by yourself. Note that it should correspond to the next src directory.
That is to say, I have a class named Cyservice under the package of cn.smileyan.cy, which is the key class of Ajax method and background communication.
& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt
& lt! DOCTYPE DWR PUBLIC "-//get head Limited//DTD Direct Web Remoting 3.0//EN " " smile Yan . cy . cy service "/& gt;
& lt/create & gt;
& lt/allow & gt;
& lt/DWR & gt;
(3) Add DWR configuration in web.xml
& ltservlet & gt
& lt display name & gtDWR Servlet & lt; /display-name & gt;
& ltservlet-name & gt; dwr-invoker</servlet-name & gt;
& ltservlet-class & gt; org . direct web remoting . servlet . dwrservlet & lt; /servlet-class & gt;
& ltinit-param >
& ltparam-name & gt; Debugging & lt/param-name >
& lt parameter value & gt & gttrue & lt/param-value & gt;;
& lt/init-param & gt;
& lt/servlet & gt;
& ltservlet mapping & gt
& ltservlet-name & gt; dwr-invoker</servlet-name & gt;
& lturl mode & gt/DWR/* <; /URL-pattern & gt;
& lt/servlet-mapping & gt; 3. Front-end code
Special reminder, you need to pay attention to the introduction of DWR script code, remember to correspond to Demo.js
& lt% @ page language = " Java " content type = " text/html; charset=ISO-8859- 1 "
page encoding = " ISO-8859- 1 " % & gt;
& lt! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 1 Transitional//EN " " smile Yan . cy;
Public class Cyservice {
Common string get(String str) {
Answer "Hello, DWR!" ;
}
} 5. Run, and then click the button to see the effect.
Fourth, summary.
This example is very simple, but it is different from the short answer js implementation with hidden display, because it actually converts java code into js code, that is, the Demo.js and Demo.get methods corresponding to the Cyservice class are actually the get methods of the objects calling Cyservice.
The advantage of this is that it realizes the interaction with the background without jumping, which greatly improves the user experience.
Related suggestions:
DWR framework experience (no refresh effect based on ajax)