实现动态检验用户是否在数据库中存在.
一:JSP页面
<script type="text/javascript" src="JS/Ajax.js"
<script type="text/javascript">
function
if(value!=""){
"get","check?usersName="+value,deal,null);
}
}
</script>
<form action="register" method="post" id="form1">
<table>
<tr>
<td>用户名:</td>
<td>
"text" name="usersName" οnblur="checkUsersName(this.value)"/>
"check">这里显示检测结果信息</div>
</td>
</tr>
<tr>
<td>
"submit" value="注册"/>
</td>
<td>
"reset" value="重置"/>
</td>
</tr>
</table>
</form>
二:Action接收请求并处理,并返回结果
public String checkUserName()
{
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
try {
out=response.getWriter();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String name="";
try {
//页面传递usersName过来,这里涉及到中文转码
name=java.net.URLDecoder.decode(usersName,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//存在返回1,否则返回0
if(rs.checkUserExit(name)!=null) // 进行数据库校验
{
exit="1";
out.print(exit);
out.flush();
out.close();
System.out.println(exit);
}
return null;}
三:Struts配置文件
<action name="check" class="com.article.action.RegisterAction" method="checkUserName">
<result>register.jsp</result>
</action>
上面的<result></result>里面可以不写,如果报错,就写个已存在的页面,不会跳转的,注册页面与action交互,action里execute的返回值必须为空!
标签:name,usersName,ajax,value,struts2,catch,null,交互,out From: https://blog.51cto.com/u_16230604/7554812