dwr 实现步骤

第一步:导dwr.jar,commons-logging-1.0.4.jar,
commons-logging-adapters-1.1.jar,
commons-logging-api-1.1.jar
第二步:写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
   <display-name>DWR Servlet</display-name>
  <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>
</web-app>

第三步:写dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
   "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
   "http://getahead.org/dwr/dwr20.dtd">
<dwr>
  <allow>
    <create creator="new" javascript="Demo">
      <param name="class" value="com.Demo"/>
    </create>
  </allow>
</dwr>
第四步:写Demo.java类
package com;
public final class Demo
{
public String hello(String name)
{
   if(name.equals("tangleboy")){
   return name + "你的dwr程序成功运行!";
   }else{
   return "输入信息错误!";
   }
}
}
第五步:写index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <script type='text/javascript' src='dwr/interface/Demo.js'></script>
     <script type='text/javascript' src='dwr/engine.js'></script>
     <script type='text/javascript' src='dwr/util.js'></script>
     <script type='text/javascript' src='hello.js'></script>
      <script type="text/javascript">
      function hello() {
  var name = dwr.util.getValue("name");
  Demo.hello(name, a);
}
function a(data) {
    dwr.util.setValue("result", data);
}
      </script>
  </head>
  <body>
     <input type="text" name="name" value="tangleboy!">
  <input type="button" value="button" onclick="hello();">
  <span id="result" ></span>
  </body>
</html>

你可能感兴趣的:(JavaScript)