DWR is an AJAX framework, in the JS code can easily call Java class method, DWR and Ajax What difference, DWR configuration more, JS write less code, AJAX does not need any configuration, write code more
How do I use the DWR framework?
Download Dwr30.jar,common-logging.jar First, put it under Lib
Create a dwr.xml under Web-inf
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE dwr Public " -//getahead limited//dtd Direct Web Remoting 3.0//en " http://getahead.org/dwr/dwr30.dtd" ><dwr> <allow> <create creator= "new" javascript= "Dwrservice" > <param Name= "class" value= " Com.chen.Chen "></param> </create> </allow></dwr>
Creator is created in New way, JavaScript represents a function called Java in the JS page identifier, param tag specifies the class that the identifier calls
The configuration in Web. XML is
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 "> <servlet> <servlet-name>dwr-invoke</servlet-name> <servlet-class>org.directwebremoting.servlet.dwrservlet</servlet-class> <!--Configure DWR's debug functionality for easy testing, and when released, be sure to delete--<init-param> <param-name>debug</param- Name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-in Voke</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <WELCOME-FILE>INDEX.JSP&L T;/welcome-file> </welcome-file-list></web-app>
Java class
Package Com.chen; Public class Chen { public string getName (string name) { System.out.println ("a"); return name + "abc"; } }
Page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >var callservicemethod=function (obj) {//parameter 1 is a parameter that needs to be given to a Java class method//the last parameter is the callback methodDwrservice.getname (' abc '), function (data) {alert ("B") alert (data); }); }; Callservicemethod ();</script>ABC</body>Use of the DWR framework