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...
  • vbubble
  • vbubble
  • 2011年12月13日 16:09
  • 895

jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互

AJAX概述 1.什么是Ajax? Ajax是由Jesse James Garrett创造的,是“Asynchronous JavaScript + XML的简写”。 2.Adapti...

使用Ajax验证用户名是否存在

现在很多的网站,多支持用户名不能重复的功能,即在用户注册时需要验证,验证其用户名不能重复使用,这样用户名其实也成为了唯一的主键,方便了项目后面许许多多的开发。...

AJAX与PHP(PHP笔记)--动态验证用户名

在PHP基础的学习过程中经常会遇到对页面的局部刷新。比如说,我们在填写用户名的同时,对数据库中的信息进行验证,检查是否充分。这时就要用到AJAX实现页面的动态加载。下面例子是简单的PHP与AJAX交互...

ajax用户名验证简例

  • 2008年03月14日 23:33
  • 107KB
  • 下载

Ajax验证用户名是否存在例

  • 2008年12月28日 16:39
  • 5KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ajax对用户名进行验证
举报原因:
原因补充:

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