使用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 ...
随机推荐
- svn 使用TortoiseSVN server搭建本地SVN服务器
使用TortoiseSVN server搭建本地SVN服务器
- SpringMVC demo 小例子,实现简单的登录和注册
1.创建一个动态的web工程 2.导入springMvc所需要的jar包(这里可以去网上找,资源有很多) 前两部就不详细描述了,后面才是正经代码~ 首先有一个web.xml文件,这个属于大配置文件,由 ...
- Android第三方开源图片裁剪截取:cropper
Android第三方开源图片裁剪截取:cropper 很多app都需要裁剪截取图片作为头像.logo之类,而cropper是github上的一个针对Android平台的.第三方开源图片裁剪截取项 ...
- 51Nod——T 1113 矩阵快速幂
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 ...
- N天学习一个linux命令之lsof
用途 列出进程已打开的文件,文件可以是常规文件,特殊文件,目录,socket,设备,共享库等.如果不带参数,lsof显示所有进程打开的所有文件. 用法 lsof [ -?abChlnNOPRtUvVX ...
- IDFTP连不上FTP服务器的解决方法
使用IDFTP连不上FTP服务端. 原来是IDFTP有2种模式:主动模式和被动模式. IDFTP默认是使用主动模式,如果FTP服务端设置使用被动模式,使用IDFTP连不上FTP服务端的. 解决方法: ...
- 前台JSON字符串,spring mvc controller也接收字符串
前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...
- 大型项目开发: 隔离 (《大规模C++程序设计》书摘)
书中第六章 隔离. 主要在撰述什么须要定义在头文件?什么应当移到编译单元中? 核心仍然是先区分接口定义与实现细节.实现细节的改变会导致客户代码的又一次编译,从逻辑上也表示与客户代码间可能存在着强耦合. ...
- CocoaPods建立私有仓库
项目管理:CocoaPods建立私有仓库 2015-05-08 10:22 编辑: lansekuangtu 分类:iOS开发 来源:agdsdl 0 6367 CocoaPods项目管理私有仓库 招 ...
- Hdu oj 1012 u Calculate e
分析:注意格式. #include<stdio.h> int main() { int i,j,k; double sum=0; printf("n e\n- --------- ...