A simple DWR example

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.