What is DWR?
DWR is a Java project of open source. DWR allows JavaScript to call Java programs running on the Web server. Easy Ajax for Java.
The following describes how to complete a simple demo step by step.
1. Download The DWR https://dwr.dev.java.net/files/documents/2427/32252/dwr.war from the official website and put it directly under tomcat_home/webapps.
2. Write a Java class as follows:
Dwrtest. Java
Package com. Test. Ajax;
/**
*
* @ Author Fang Jian Hua
*
*/
Public class dwrtest {
Public dwrtest (){
}
Public String getmyname (){
Return "James Fang ";
}
}
3. Configure the drwtest class in DWR. xml. DWR. XML is the configuration file of DWR. All Java classes that need to be called in Javascript must be described in this file. The file is located at atat_home/webapps/DWR/WEB-INF/DWR. xml. Add the following content to the <allow> label:
<Create creator = "new" javascript = "mytest" Scope = "application">
<Param name = "class" value = "com. Test. Ajax. dwrtest"/>
</Create>
4. Compile the HTML file test.html that calls the "dwrtest" class "getmyname '.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<Head>
<Title> DWR-my first Ajax </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<SCRIPT type = 'text/JavaScript 'src = '/DWR/interface/mytest. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = '/DWR/engine. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = '/DWR/util. js'> </SCRIPT>
<Script language = "JavaScript">
VaR mycall = function callback (data ){
Dwrutil. setvalue ("demo1", data );
}
Function showmyname (){
Mytest. getmyname (mycall );
}
Function clearname (){
Demo1.value = "";
}
</SCRIPT>
</Head>
<Body>
<H1> Ajax test page
<Input type = "button" value = "setmyname" onclick = "javascript: showmyname ()"> <input type = "button" value = "clear" onclick = "javascript: clearname () "> <br>
<Input type = "text" id = "demo1"> </SELECT> <br>
</Body>
5. Run. Enter http: // localhost: 8080/DWR/test.html in the browser.