使用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 ...
随机推荐
- 【7】Django网页视图模板处理
天下难事必作於易.天下大事必作於细.是以圣人终不为大,故能成其大 --老子<道德经> 本节内容 HTML页面的渲染 使用页面模板 异常处理 超链接路径处理 路由命名空间 1. HTML页面 ...
- c# 用binary实现序列化和反序列化
直接用实例来说明序列化和反序列化: namespace DynamicTest{ class Program { static void Main(string[] args) { List<P ...
- Vue项目搭建及原理四
四.Vue-cli工作原理及Vue实例创建,工作原理 (一)Vue-cli原理 1.webpack其实使用了node.js的express网页服务器来进行处理网页相关的数据,相当于使用一个类似apac ...
- [luoguP3275] [SCOI2011]糖果(差分约束)
传送门 差分约束裸题 但是坑! 有一个点是长为10W的链,需要逆序加边才能过(真是玄学) 还有各种坑爹数据 开longlong ——代码 #include <cstdio> #includ ...
- 车展(vijos P1459)
描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...
- ubuntu网卡ip的配置
ifconfig 命令的结果 和 ip addr (或者查看具体网卡的是 ip addr show eth0) 看到的结果不一样, ip addr show eth0 可以看到eth0网卡上面的多个 ...
- Navicat 提示Cannot create oci environment 解决方式
一直在使用Navicat,这是一个数据库client软件.能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷.近期.我在电脑上安装了orcale,然后,Navicat就莫名其妙的不能连接o ...
- UIView 的图层关系
个人认为用字母取代这样的比較好理解,.给新人学习 addSubview是一层一层往上加,新加的仅仅能放到父视图的最上层, insertSubView能够控制它加入到父视图的哪一层 A addSubv ...
- double型转换成string型
double型转换成string型 题目描写叙述: 如有一个函数.其可接受一个long double參数,并将參数转换为字符串.结果字符串应保留两位小数,比如,浮点值123.45678应该生成&quo ...
- linux下apache+openssl配置记录
软件环境 Apache Httpd 2.2.29 (http://httpd.apache.org ) OpenSSL 1.0.1h (http://www.openssl.org/source ) ...