已经输出 用户名 在数据库中 就是在 a 返回到Ajax中 没收到 这是为什么
register.jsp
<form name="form1" method="post" action="register.action" ">
<input type="text" id="txt" onKeyUp="chk()" onbeforepaste="chk()" name="user.name" onBlur="checkName(this.value);"> <span class="d" id="divname"></span><input type="submit" name="Submit" id="Submit" value="提交" onclick="submit()" / style="border: 1px solid #999999; width:100px; background-color:#0b66e3;" />
</form>
ajax var xmlHttp;
//实例化xmlHttp
function init()
{
try{
xmlHttp= new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e)
{
try{
xmlHttp= new ActiveXObject('Microsoft.XMLHTTP');
}
catch(e)
{
try{
xmlHttp= new XMLHttpRequest();
}catch(e){}
}
}
}
//注册验证账号
//var name1
function checkName(name1){ init();
//name1=document.getElementById("txt").value;
document.getElementById("divname").innerHTML="";
if(name1.length=="")
{
document.getElementById("divname").innerHTML="必须输入用户名";
document.getElementById("txt").focus();
return false;
}else if(name1.length>12||name1.length<3){
document.getElementById("divname").innerHTML="账号长度必须在3—-12之间!";
document.getElementById("txt").focus();
return false;
}
var url="vname.action?name1="+name1;
alert(url);
xmlHttp.open("get",url,true);
xmlHttp.onreadystatechange=handleCheckName;
xmlHttp.send(null);
}function handleCheckName()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="1")
{
document.getElementById("divname").innerHTML="对不起,用户已存在!";
//alert("必须输入正确的用户名。");
name1.focus();
}if(xmlHttp.responseText=="2")
{
document.getElementById("divname").innerHTML="恭喜你,可以继续注册!";
}
}
}
}
struts.xml
<action name="vname" class="mx.action.UserAction" method="isName">
<result name="success">/index.jsp</result>
<result name="input">/register.jsp</result>
</action>
UserAction.java
public String isName() throws SQLException, IOException{
UserDAO dao = new UserDAO();
PrintWriter pwr=ServletActionContext.getResponse().getWriter();
String name=ServletActionContext.getRequest().getParameter("name1");
int a=0;
if(dao.isName(name)){
a=1;
}else{
a=2;
} pwr.print(a);
System.out.println("a"+a);
return SUCCESS;
}
UserDAO.java
public boolean isName(String name) throws SQLException, IOException {
boolean boo = false;
Connection con = null;
String sql = "select * from _user where name=? ";
PreparedStatement ps = null;
ResultSet rs;
try {
con=DatabaseConnection.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,name);
//ps.setString(2,user.getPassword());
rs =ps.executeQuery();
if(rs.next()){
boo=true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return boo;
}
register.jsp
<form name="form1" method="post" action="register.action" ">
<input type="text" id="txt" onKeyUp="chk()" onbeforepaste="chk()" name="user.name" onBlur="checkName(this.value);"> <span class="d" id="divname"></span><input type="submit" name="Submit" id="Submit" value="提交" onclick="submit()" / style="border: 1px solid #999999; width:100px; background-color:#0b66e3;" />
</form>
ajax var xmlHttp;
//实例化xmlHttp
function init()
{
try{
xmlHttp= new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e)
{
try{
xmlHttp= new ActiveXObject('Microsoft.XMLHTTP');
}
catch(e)
{
try{
xmlHttp= new XMLHttpRequest();
}catch(e){}
}
}
}
//注册验证账号
//var name1
function checkName(name1){ init();
//name1=document.getElementById("txt").value;
document.getElementById("divname").innerHTML="";
if(name1.length=="")
{
document.getElementById("divname").innerHTML="必须输入用户名";
document.getElementById("txt").focus();
return false;
}else if(name1.length>12||name1.length<3){
document.getElementById("divname").innerHTML="账号长度必须在3—-12之间!";
document.getElementById("txt").focus();
return false;
}
var url="vname.action?name1="+name1;
alert(url);
xmlHttp.open("get",url,true);
xmlHttp.onreadystatechange=handleCheckName;
xmlHttp.send(null);
}function handleCheckName()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(xmlHttp.responseText=="1")
{
document.getElementById("divname").innerHTML="对不起,用户已存在!";
//alert("必须输入正确的用户名。");
name1.focus();
}if(xmlHttp.responseText=="2")
{
document.getElementById("divname").innerHTML="恭喜你,可以继续注册!";
}
}
}
}
struts.xml
<action name="vname" class="mx.action.UserAction" method="isName">
<result name="success">/index.jsp</result>
<result name="input">/register.jsp</result>
</action>
UserAction.java
public String isName() throws SQLException, IOException{
UserDAO dao = new UserDAO();
PrintWriter pwr=ServletActionContext.getResponse().getWriter();
String name=ServletActionContext.getRequest().getParameter("name1");
int a=0;
if(dao.isName(name)){
a=1;
}else{
a=2;
} pwr.print(a);
System.out.println("a"+a);
return SUCCESS;
}
UserDAO.java
public boolean isName(String name) throws SQLException, IOException {
boolean boo = false;
Connection con = null;
String sql = "select * from _user where name=? ";
PreparedStatement ps = null;
ResultSet rs;
try {
con=DatabaseConnection.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,name);
//ps.setString(2,user.getPassword());
rs =ps.executeQuery();
if(rs.next()){
boo=true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return boo;
}
public class UserActionextends ActionSupport{
public String execute() {
HttpServletResponse response = ServletActionContext.getResponse();
response.getWriter().write("存在用户");
return null;
}
}