Ajax对用户名进行验证

原创 2015年07月07日 23:20:45



//JSP页面部分:
<%@ page language="java" pageEncoding="utf-8"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>校验用户名是否存在</title>
</head>
<body>
<center>
<form action="" enctype="application/x-www-form-urlencoded">
<h3>请填写用户注册信息</h3>
<table border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="" id="username">
<div id="divcheck"></div>
<input type="button" name="checkusername" value="查看用户名" id="checkusername"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="psw" value=""></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="confpsw" value=""></td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="birthday" value=""></td>
</tr>
</table>
</form>
</center>
<script type="text/javascript">
window.onload=function(){
document.getElementById("checkusername").onclick=function(){
var xhr = createXmlHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200||xhr.status==304){
var data = xhr.responseText;
document.getElementById("divcheck").innerHTML=data;
}
}
}
xhr.open("POST","${pageContext.request.contextPath}/servlet/ServletDemo3?time="+new Date().getTime());
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var username = document.getElementById("username").value;
xhr.send("username="+username);
}
}
function createXmlHttpRequest(){
var xmlHttp;
try{ //Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
try{ //Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
return xmlHttp;
}
</script>
</body>
</html>


//servlet部分:
package cn.itcast.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
* Servlet implementation class ServletDemo3
*/
@WebServlet("/servlet/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
System.out.println(username);
if(username.equals("sa")){
out.write("用户名已经存在");
}else{
out.write("用户名可以使用");
}
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
 
}


//JSP页面部分:
<%@ page language="java" pageEncoding="utf-8"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>校验用户名是否存在</title>
</head>
<body>
<center>
<form action="" enctype="application/x-www-form-urlencoded">
<h3>请填写用户注册信息</h3>
<table border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="" id="username">
<div id="divcheck"></div>
<input type="button" name="checkusername" value="查看用户名" id="checkusername"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="psw" value=""></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="confpsw" value=""></td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="birthday" value=""></td>
</tr>
</table>
</form>
</center>
<script type="text/javascript">
window.onload=function(){
document.getElementById("checkusername").onclick=function(){
var xhr = createXmlHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200||xhr.status==304){
var data = xhr.responseText;
document.getElementById("divcheck").innerHTML=data;
}
}
}
xhr.open("POST","${pageContext.request.contextPath}/servlet/ServletDemo3?time="+new Date().getTime());
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var username = document.getElementById("username").value;
xhr.send("username="+username);
}
}
function createXmlHttpRequest(){
var xmlHttp;
try{ //Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
try{ //Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
return xmlHttp;
}
</script>
</body>
</html>


//servlet部分:
package cn.itcast.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
* Servlet implementation class ServletDemo3
*/
@WebServlet("/servlet/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
System.out.println(username);
if(username.equals("sa")){
out.write("用户名已经存在");
}else{
out.write("用户名可以使用");
}
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
 
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

使用jqeury+Ajax进行用户名验证

准备jquery库! 注册页面! 注册 function UserNameCheck() { $.ajax({ typ...

php与ajax用户名验证

checkuserreg.php header('Content-Type:text/html;charset=GB2312');//避免输出乱码 $dbhost     = "local...

ajax action 用户名异步验证

jsp 页面 "> My JSP 'Ajax.jsp'

ajax 实现无刷新验证用户名是否存在

html代码: $(function () { $('#txtname').blur(fun...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)