ajax+struts实现用户名验证功能(原创)
时间:2006-05-31 来源:dxadnwfn
最近我在学ajax,看了一些例子。自己也改了一个。 用ajax+struts
实现用户名验证功能。给大家分享一下,哪里要改的请回复交流。。。
存在问题:document.getElementById("load").style.display='';这里火狐不能执行,这是怎么回事。。知道请帮个忙啊。。。加我的QQ也可以:313900185 页面端:
<%@ page contentType="text/html; charset=gb2312" %>
<% request.setCharacterEncoding("gb2312"); %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/jUtil.tld" prefix="jutil"%>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title> </head> <body>
<form action="" method="post" name="form1">
<table width="564" border="0">
<tr>
<td width="89">用户名:</td>
<td width="465"><input name="username" type="text" id="username" onBlur="chkUser()"> <div id=load style="display:none; position:absolute;right:0px;top:0px;background:#FF5B5B;border:1px solid">正在验证用户名,请稍后……</div></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="15"> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
struts-congfig.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config> <form-beans >
<form-bean name="homeForm" type="com.trsoft.home.HomeForm"/>
</form-beans>
<global-forwards> </global-forwards>
<action-mappings>
<action path="/webapp/home"
type="com.trsoft.home.HomeAction"
name="homeForm"
scope="request"
validate="false">
<forward name="home" path="/webapp/homepage/homepage.jsp"/>
</action>
</action-mappings>
</struts-config> 程序实现: package com.trsoft.home; /**
* @作者:gong cheng wei
*
* @创建时间:2006-05-25
*
* @功能:返回主页上信息
*
* @request参数意义:
* action 判断具体执行哪一个功能块..
*
* @定义的变量意义:
* String forward="";返回地址
* ArrayList list :记录集的存放
* @被调用的类:
* com.trsoft.home.HomeBean;
*
*/
import java.io.UnsupportedEncodingException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList; public class HomeAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException{
request.setCharacterEncoding("gb2312");
String action=(String)request.getParameter("action");
String forward="";
/**
* @功能:判断用户是否已注册
*
* @参数:
* String username用户名
*
*
* @流程:
*
*/
else if("verdictuserName".equals(action)){
String msgStr="";
try{
response.setContentType("text/html;charset=GB2312");
String userName=(String)request.getParameter("userName");
System.out.println("userName = "+userName);
if("gong".equals(userName)){ //找查数据库中有无该用户名
msgStr ="对不起,此用户名已经存在,请更换用户名注册!";
}else{
msgStr ="用户未被注册,可以使用";
}
response.getWriter().write(msgStr);
}catch(Exception ex){
}
}
/**
* @功能:用户注册
*
* @参数:
* String username用户名
* String password 密码
*
* @流程:
*
*/
else if("register".equals(action)){
}
return mapping.findForward(forward);
}
}
相关阅读 更多 +