使用Ajax验证用户名
Ajax是一项很重要的技术,下面简要举个例子,来解释如何使用Ajax。步骤如下:
使用Ajax验证用户名
使用文本框的onBlur事件
使用Ajax技术实现异步交互
创建XMLHttpRequest对象
通过 XMLHttpRequest对象设置请求信息
向服务器发送请求
创建回调函数,根据响应状态动态更新页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>异步验证用户名</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" id="name" onblur="change()"/></td>
<td><span style="display: none;color: red" id="span"></span></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" id="password" /></td>
</tr>
<tr> <td><input type="button" value="提交" onclick="change()"></td>
</tr> </table> </body>
<script type="text/javascript">
//使用异步刷新技术实现验证用户名 //声明全局对象
var xmlhttp;
//第一步:创建ck对象
function ck(){
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest;
}else if(window.ActiveObject){
xmlhttp=new ActiveObject("Microsoft.XMLHTTP")
} } //第二步:响应鼠标事件
function change(){
//获取用户名
var username=document.getElementById("name");
var password = document.getElementById("password"); if(!username.value){
alert("用户名不能为空");
}else if(!password.value){
alert("密码不能为空!!!") }else{ //请求异步刷新;以请求地址作为参数传递
doAjax("select/doselect?username="+username.value+"password="+password.value);
}
}
//第三步:获取URL
function doAjax(url){
//初始化ck;
ck();
//判断对象是否初始化成功
if(xmlhttp!=null){
//如果不为空,则说明初始化成功
//开始请求服务器
xmlhttp.open("post",url,true);//初始化请求参数
xmlhttp.onreadystatechange=ok;//指定回调函数
xmlhttp.send(null); }else{
alert("xmlhttp初始化失败!!!");
}
}
//指定回调函数
function ok(){
//判断响应状态
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
chuli(xmlhttp.responseText);
}
} }
function chuli(status){
var span=document.getElementById("span");
span.innerHTML=status;
span.style.display="block"; } </script>
</html>
JAVA代码Servlet语法:
javapackage select; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class doselect extends HttpServlet { /**
* Constructor of the object.
*/
public doselect() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("请求正常"); String[] user = new String[]{"胖胖","薇薇","黄鼠狼","李四"}; String status = "用户名可以使用!";//状态:如果为0则表示该用户名不存在;否则用户名已占用 //得到请求参数
String name = request.getParameter("username");
name = new String(name.getBytes("ISO-8859-1"),"GBK"); for (int i = 0; i < user.length; i++) {
if(name.equals(user[i])){
status = "用户名已存在!";
}
} //将信息返回客户端
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println(status); out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
在Java_Web项目的xml文档配置具体路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>doselect</servlet-name>
<servlet-class>select.doselect</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>doselect</servlet-name>
<url-pattern>/select/doselect</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
使用Ajax验证用户名的更多相关文章
- 11月10日上午ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库
1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...
- 利用jQuery实现的Ajax 验证用户名是否存在
异步刷新实现方式有多种,也可以借助JS的多种框架,下面是使用jQuery框架实现的AJAX 验证用户名是否存在 jQuery.ajax概述 HTTP 请求加载远程数据. 通过jQuery 底层 AJA ...
- ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库
1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...
- Ajax验证用户名是否被注册
Ajax验证用户名是否被注册 var xmlHttp; function createXMLHttpRequest(){ // 创建XMLHttp请求对象 if(window.ActiveXObjec ...
- 11.10 (下午)开课二个月零六天(ajax验证用户名,ajax调数据库)
用ajax验证用户名是否可用 testuid.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...
- 基于jQuery实现的Ajax 验证用户名唯一性
基于jQuery实现的Ajax 验证用户名唯一性 前端jsp页面代码 <tr> <th><span class="requiredField"> ...
- 14.ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库
1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...
- Ajax验证用户名
用Ajax验证用户名: 接口: get guestbook/index.php m : index a : verifyUserName username : 要验证的用户名 返回 { code : ...
- ajax验证用户名 当用户名框的数据改变时 执行ajax方法
ajax验证用户名 当用户名框的数据改变时 执行ajax方法 <html xmlns="http://www.w3.org/1999/xhtml" ><head ...
随机推荐
- 小白神器 - 一篇博客学会HTML
小白神器 - 一篇博客学会HTML 一. 简介 1. HTML 定义 htyper text markup language 即超文本标记语言. 超文本: 就是指页面内可以包含图片.链接,甚至音乐. ...
- Linux - docker基础
目录 Linux - docker基础 docker的概念 docker安装流程 docker基本命令学习 docker 的 hello docker 运行一个ubuntu容器 Docker与Cent ...
- 【codeforces 514B】Han Solo and Lazer Gun
[题目链接]:http://codeforces.com/contest/514/problem/B [题意] 每次攻击可以把经过自己的一条直线上的所有点都毁掉; 然后给你n个目标物的坐标 问你最少要 ...
- ZooKeeper的原理(转)
一.ZooKeeper的角色 领导者(Leader),负责进行投票的发起和决议,更新系统状态. 学习者(Learner),包括跟随者(Follower)和观察者(Observer),Follower用 ...
- Axure RP一个专业的高速原型设计工具
Axure RP是一个专业的高速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司.RP则是Rapid Prototyping(高速原型)的缩写. Axure简要介绍 Axur ...
- php file_get_contents遇到https的处理办法
最近调整了文件上传后的资源路径,导致pageOffice在线编辑功能无法正常使用,每一次打开文件都报错:该文件为0字节.仔细看了一下程序,整理逻辑流程如下图: 增加日志后发现,保存在服务器路径下的该条 ...
- iOS学习(3)
4. 这个写法会出什么问题: @property (copy) NSMutableArray *array; 两个问题:1.加入,删除,改动数组内的元素的时候,程序会由于找不到相应的方法而崩溃.由于 ...
- Recovery 中的UI知识积累【转】
本文转载自:http://blog.csdn.net/wed110/article/details/26554197 int gr_init(void); /* 初始化图形显示 ...
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- IJ-Error:常见错误
ylbtech-IJ-Error:常见错误 1.返回顶部 1. This application has no explicit mapping for /error, so you are seei ...