使用Ajax实现登录验证

get提交方式:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

	<head>
		<title>My JSP 'loginAjax.jsp' starting page</title>
		<style type="text/css">
			body {
				text-align: center;
				background-color: #E0FFFF;
			}
			
			span {
				color: darkorange;
			}
		</style>
	</head>

	<body>
		<form action="">
			用户名:<input type="text" id="uname" name="uname" maxlength="5" /><span id="tip"></span><br /> 密 码:<input type="text" name="pwd" maxlength="10" /><span id="tip2"></span><br /> <input type="submit" value="提交" />
		</form>
		<br>
	</body>
	<script type="text/javascript">
		document.getElementById("uname").onblur = function() {
			var uname = this.value;
			if(uname.length == 0) {
				document.getElementById("tip").innerHTML = "用户名必填";
			} else {
				//uname = URLEncoder();
				//alert(uname);
				var ajax = createAjax();

				function createAjax() {
					var ajax = null;
					try {
						ajax = new ActiveXObject("microsoft.xmlhttp");
					} catch(e) {
						ajax = new XMLHttpRequest();

					}
					return ajax;
				}
				ajax.open("get", "${pageContext.request.contextPath}/LoginServlet?time=" + new Date().getDate() + "&username=" + uname);
				ajax.send(null);
				ajax.onreadystatechange = function() {
					if(ajax.readyState == 4) {
						if(ajax.status == 200) {
							var varReturn = ajax.responseText;
							document.getElementById("tip").innerHTML = varReturn;
						}
					}
				}
			}

		}
	</script>

</html>

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String un = request.getParameter("username");
		byte[] by = un.getBytes("ISO8859-1");
		un = new String(by, "UTF-8");
		String str = "用户名可以注册";
		System.out.println(un);
		if ("张三".equals(un)) {
			str = "用户名已经注册";
			System.out.println("zs");
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.print(str);
		pw.flush();
		pw.close();

	}
}

post提交方式

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>My JSP 'loginAjax.jsp' starting page</title>
<style type="text/css">
body {
	text-align: center;
	background-color: #E0FFFF;
}

span {
	color: darkorange;
}
</style>
</head>

<body>
	<form action="">
		用户名:<input type="text" id="uname" name="uname" maxlength="5" /><span
			id="tip"></span><br /> 密 码:<input type="text" name="pwd"
			maxlength="10" /><span id="tip2"></span><br /> <input type="submit"
			value="提交" />
	</form>
	<br>
</body>
<script type="text/javascript">
	document.getElementById("uname").onblur = function() {
		var uname = this.value;
		if (uname.length == 0) {
			document.getElementById("tip").innerHTML = "用户名必填";
		} else {
			//uname = URLEncoder();
			//alert(uname);
			var ajax = createAjax();

			function createAjax() {
				var ajax = null;
				try {
					ajax = new ActiveXObject("microsoft.xmlhttp");
				} catch (e) {
					ajax = new XMLHttpRequest();

				}
				return ajax;
			}
			ajax.open("POST",
					"${pageContext.request.contextPath}/AjaxTimeServlet2?time="
							+ new Date().getDate());
			ajax.setRequestHeader("content-type",
					"application/x-www-form-urlencoded");
			ajax.send("username=" + uname);
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.status == 200) {
						var varReturn = ajax.responseText;
						//document.getElementById("tip").innerHTML = varReturn;
						var imaElement = document.createElement("img");
						//alert(varReturn);
						imaElement.src = varReturn;//src为servlet里面返回的字符串图片路径
						imaElement.style.width = "12px";
						imaElement.style.height = "12px";
						var spanElement = document.getElementById("tip");
						//清空span标签中的内容
						spanElement.innerHTML = "";
						//将img标签加入到span标签中
						spanElement.appendChild(imaElement);
					}
				}
			}
		}

	}
</script>

</html>

public class AjaxTimeServlet2 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 
		response.setContentType("text/html;charset=UTF-8");
		String un = request.getParameter("username");
		byte[] by = un.getBytes("ISO8859-1");
		un = new String(by, "UTF-8");
		System.out.println(un);
		String img = "images/MsgSent.gif";
		System.out.println(un);

		if ("张三".equals(un)) {
			img = "images/MsgError.gif";
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(img);
		pw.flush();
		pw.close();
	}

}


 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值