Ajax+Stryts测试实例

来源:互联网 发布:ios付费软件退款 编辑:程序博客网 时间:2024/06/08 10:47

JSP页面:
<%@ page contentType="text/html; charset=GBK" %>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function testName(){
createXMLHttpRequest();
xmlHttp.onreadystatechange = callBack;
xmlHttp.open('POST', '/peam/opOpNote.do?method=add', true);
xmlHttp.send(null);
}

function callBack() {
var view=document.getElementById("view");
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
if(xmlHttp.responseText==1){
view.innerHTML='该用户名已经被使用';
}else{
view.innerHTML='该用户名含有非法字符!';
}
}
}
}
</script>

<html>
<head>
<title>
ajax
</title>
</head>
<body bgcolor="#ffffff">
<h1>
<input name="username" type="text" maxlength="20" />
<input id="button" name="button" type="button" value="检测帐号" onclick="testName();" />
<div id="view"></div>
</h1>
</body>
</html>

Action.java文件:
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws java.lang.Exception {
PrintWriter out = response.getWriter();
out.print(1);
return null;//ajax取得都是字符的输出。如果数据量大的话,还可以用xml来发送和接受

}

 
原创粉丝点击