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> <servlet-name>dwr</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</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </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部分定义了DWR能够创建和转换的类。 --> <allow> <!-- 这个creator将使用默认构造器创建类的实例 --> <!-- 这个javascript用于指定浏览器中这个被创造出来的对象的名字,不能使用Javascript的关键字。 --> <create creator="new" javascript="loginBiz"> <param name="class" value="com.heyuanbo.biz.LoginBiz" /> <!-- 只允许调用login方法 --> <include method="login" /> <!-- 除了login方法,其他的方法都可以调用 --> <!-- <exclude method="login" /> --> </create> </allow> </dwr> loginBiz.java: package com.heyuanbo.biz; public class LoginBiz { public String login(String userName, String password) { String message = "登录失败!"; if("heyuanbo".equals(userName) && "123456".equals(password)){ message = "登录成功!"; } return message + userName; } } login.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户登录</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> <mce:script type='text/javascript' src="dwr/util.js" mce_src="dwr/util.js"></mce:script> <mce:script type='text/javascript' src="dwr/engine.js" mce_src="dwr/engine.js"></mce:script> <mce:script type='text/javascript' src="dwr/interface/loginBiz.js" mce_src="dwr/interface/loginBiz.js"></mce:script> <!-- <mce:script type='text/javascript' src="js/jQuery/jQuery.js" mce_src="js/jQuery/jQuery.js"></mce:script> <mce:script type='text/javascript' src="js/common.js" mce_src="js/common.js"></mce:script> --> <mce:script type="text/javascript"><!-- function checkLogin(){ var userName = document.getElementById("userName").value; var password = document.getElementById("password").value; loginBiz.login(userName, password, callBackHello); } function callBackHello(data){ alert(data); } // --></mce:script> </head> <body> <h3>用户登录</h3> <form action="" name="loginForm"> 用户名: <input type="text" name="userName" id="userName" value="" /> 密码: <input type="text" name="password" id="password" value="" /> <input type="button" name="button" value="登录" οnclick="checkLogin()"> </form> </body> </html> 效果图: