使用dwr框架实现Ajax(登录用户名验证)
1. 导入dwr.jar
2. 修改web.xml,添加代码
<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>
3. 在/WEB-INF/下创建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="JUserChecker">
<param name="class" value="t.UserManager" />
<include method="check" />
</create>
</allow>
</dwr>
代码:
package t;
publicclassUserManager {
publicboolean check(String uname){
if(uname.equals("f")) returntrue;
elsereturnfalse;
}
}
4. 在jsp页面中使用
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>JSP for LoginForm form</title>
<script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script>
function checkU(oCtl){
var uname=oCtl.value;
JUserChecker.check(uname,haolejiaowo);
}
function haolejiaowo(isExist){
if(isExist==1){
alert("hao");
}
else if(isExist==0){
alert("error");
}
}
</script>
</head>
<body>
<form action="" name="form1">
<input name="uname" onblur="checkU(this)">
</form>
</body>
</html>
使用Ajax技术实现页面动态添加下拉列表的值
1. 导入dwr.jar
2. 修改web.xml,添加代码
3. 在/WEB-INF/下创建dwr.xml配置文件
同上:
package t;
import java.util.ArrayList;
publicclass UserManager {
public String check(String uname){
// if(uname.equals("f")) return "是f用户";
// else return "不是";
ArrayList list=new ArrayList();
list.add("1");
list.add("2");
list.add("3");
StringBuffer sb=new StringBuffer("");
sb.append("<select name='mynum'>");
for(int i=0;i<list.size();i++){
sb.append("<option value="+(String)list.get(i)+">"+(String)list.get(i)+"</option>");
}
sb.append("</select>");
return sb.toString();
}
}
4. 在jsp页面中使用
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>JSP for LoginForm form</title>
<script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script>
function checkU(oCtl){
var uname=oCtl.value;
JUserChecker.check(uname,haolejiaowo);
}
function haolejiaowo(isExist){
document.getElementById("num").innerHTML=isExist;
}
</script>
</head>
<body>
<form action="" name="form1">
<input name="uname" onblur="checkU(this)">
<div id="num"></div>
</form>
</body>
</html>
使用Ajax技术传递List对象
1. 导入dwr.jar
2. 修改web.xml,添加代码
3. 在/WEB-INF/下创建dwr.xml配置文件
同上
package t;
import java.util.ArrayList;
import java.util.List;
publicclass UserManager {
public List check(String uname){
ArrayList list=new ArrayList();
list.add("1");
list.add("2");
list.add("3");
return list;
}
}
4. 在jsp页面中使用
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>JSP for LoginForm form</title>
<script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script>
function checkU(oCtl){
var uname=oCtl.value;
JUserChecker.check(uname,haolejiaowo);
}
function haolejiaowo(isExist){
for(i=0;i<isExist.length;i++){
alert(isExist[i]);
}
}
</script>
</head>
<body>
<form action="" name="form1">
<input name="uname" onblur="checkU(this)">
<div id="num"></div>
</form>
</body>
</html>
使用Ajax技术传递List对象,其中List封装了三个Person类的实例
1. 导入dwr.jar
2. 修改web.xml,添加代码
3. 在/WEB-INF/下创建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="JUserChecker">
<param name="class" value="t.UserManager" />
<include method="check" />
</create>
<convert match="t.Person" javascript="Person" converter="bean" />
</allow>
</dwr>
package t;
import java.util.*;
publicclass UserManager {
public List check(String uname,String upass){
ArrayList list=new ArrayList();
list.add(new Person("name1","pass1"));
list.add(new Person("name2","pass2"));
list.add(new Person("name3","pass3"));
return list;
}
}
4. 在jsp页面中使用
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>JSP for LoginForm form</title>
<script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script>
function checkU(oCtl){
var uname=oCtl.value;
JUserChecker.check(uname,haolejiaowo);
}
function haolejiaowo(isExist){
for(i=0;i<isExist.length;i++){
alert(isExist[i].uname);
}
}
</script>
</head>
<body>
<form action="" name="form1">
<input name="uname" onblur="checkU(this)">
<div id="num"></div>
</form>
</body>
</html>