六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 91|回复: 0

Ajax+Struts测试实例

[复制链接]

升级  82%

51

主题

51

主题

51

主题

秀才

Rank: 2

积分
173
 楼主| 发表于 2013-2-6 08:39:30 | 显示全部楼层 |阅读模式
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="检测帐号"  />
<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来发送和接受

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表