How DWR implements Ajax

The content of this article is about how DWR implements Ajax. It has certain reference value. Now I share it with you. Friends in need can refer to it

1. Introduction to Ajax .

AJAX = Asynchronous JavaScript and XML.

AJAX is not a new programming language, but a new way of using existing standards.

The biggest advantage of AJAX is that it can exchange data with the server and update part of the web page content without reloading the entire page.

AJAX does not require any browser plug-ins, but requires the user to allow JavaScript to execute on the browser.

2. Introduction to DWR

1. The official website address.smileyan.cy.Cyservice" can also be modified by yourself. Pay attention to the one-to-one correspondence with the src directory.

In other words, I have a class called Cyservice under the cn.smileyan.cy package. This class is the key class for Ajax methods to communicate with the background.

lt;?xml version="1.0" encoding="UTF-8"?gt;

lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" ".smileyan.cy.Cyservice"/gt;

lt;/creategt;

lt;/allowgt;

lt; /dwrgt;

(3) Add DWR configuration in web.xml

lt;servletgt;

lt;display-namegt;DWR Servletlt;/display- namegt;

lt;servlet-namegt;dwr-invokerlt;/servlet-namegt;

lt;servlet-classgt;org.directwebremoting.servlet.DwrServletlt;/servlet-classgt;

lt;init-paramgt;

lt;param-namegt;debuglt;/param-namegt;

lt;param-valuegt;truelt;/param -valuegt;

lt;/init-paramgt;

lt;/servletgt;

lt;servlet-mappinggt;

lt ;servlet-namegt;dwr-invokerlt;/servlet-namegt;

lt;url-patterngt;/dwr/*lt;/url-patterngt;

lt;/servlet- mappinggt; 3. Front-end code

Special reminder, you need to pay attention to the script code that introduces DWR, remember to correspond to Demo.js

lt; @ page language="java" contentType="text /html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"gt;

lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".smileyan.cy;

public class Cyservice {

public String get(String str) {

return "Hello DWR !";

}

} 5. Run, then click the button to see the effect

4. Summary

This example is very simple. But this is different from the simple js implementation of hiding and displaying, because this actually converts java code into js code, that is, the Cyservice class corresponds to Demo.js, and the Demo.get method actually calls the Cyservice object. get method.

The advantage of this is that it enables interaction with the background without jumping, which greatly improves the user experience.

Related recommendations:

DWR framework experience (achieving ajax-based no refresh effect)