JSP
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/engine.js">
</script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/utils.js">
</script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/interface/test.js">
</script>
<script type="text/javascript">
function checkUser(){
var uname = document.getElementById("username").value;
var pwd = document.getElementById("password").value;
test.check(uname,pwd,resultData);
}
var resultData = function(data){
if(data=="0"){
alert("用戶名或密碼錯誤");
}else{
alert("登錄成功");
}
}
</script>
</head>
<body>
DWR測試<br>
username:<input type="text" id="username"><br>
password:<input type="password" id="password"><br>
<input type="button" value="測試" onclick="checkUser()">
</body>
</html>
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 javascript="test" creator="new">
<param name="class" value="com.dwr.demo.CheckLogin"></param>
</create>
</allow>
</dwr>
WEB.xml
<servlet>
<servlet-name>dwrfilter</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>dwrfilter</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>