The simplest example of DWR

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. Simple or professional is easy Ajax for Java. Official Website: http://getahead.org/dwr, the following will be a step-by-step introduction how to complete a simple demo:Testdwr.

1. Download The DWR. jar package from the official website.

Put it directly under the Apache-Tomcat-7.0.27 \ lib directory

2. Create a WEB Project

Compile a testdwr. Java class as follows:

// Testdwr. Java

package com.test.ajax;publicclass TestDwr{       public String getMyName()    {              return"Hello Casco!";     }      }  

3. modify web. xml

// Web. xml

<?xml version="1.0" encoding="UTF-8"?>  <web-app version="3.0"       xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">    <display-name></display-name>     <servlet>      <servlet-name>dwr-invoker</servlet-name>      <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>      <init-param>          <param-name>debug</param-name>          <param-value>true</param-value>      </init-param>    </servlet>    <servlet-mapping>      <servlet-name>dwr-invoker</servlet-name>      <url-pattern>/dwr/*</url-pattern>    </servlet-mapping>    <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>  </web-app>  

4. Create a DWR. xml file

Configure the testdwr class in DWR. in XML, DWR. XML is the DWR configuration file. All Java classes that need to be called in Javascript must be described in this file (must be consistent with web. XML peer)

// DWR. xml

<?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="CASCO" scope="application"><param name="class" value="com.test.ajax.TestDwr"/></create></allow></dwr>

5. Configure references in the DWR package

Copy \ DWR \ WEB-INF \ Lib to the testdwr \ webroot \ WEB-INF \ Lib.

6. Compile JSP files

Compile the index. jsp file that calls the getmyname method in the testdwr class:

<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" pageencoding = "gb18030" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

VII. Compile and run

Enter
Http: // localhost: 8080/testdwr/, click "show name" to display the content of our function getmyname.

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.