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

2016-02-19 16:57 45 1 收藏

今天图老师小编给大家展示的是ajax实现无刷新验证用户名是否存在,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - Web开发 】

实现用的是jdbc+jsp+servlet,数据库用的是mysql

表就2个字段

实现原理获得text的值后,通过调用servlet查询数据库里是否存在,在通过xml传到前台

OperationName.java

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

response.setContentType("text/xml;charset=UTF-8")这句很重要,没有他前台获得不到xml,之前做的时候没有加

所以前台获得不到xml

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OperationName extends HttpServlet
{

/** *//**
* Constructor of the object.
*/
public OperationName()
{
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
{
String names = request.getParameter("names");
Collection students = null;
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql:"+
"//127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK","root","eetrust");
st = con.createStatement();
rs = st.executeQuery("select count(*) from test where name='"+names+"'");
StringBuffer result = new StringBuffer();
result.append("OperationNames");
System.out.println(result);
while(rs.next())
{
int num = rs.getInt(1);
result.append("OperationName" + num + "/OperationName");
}
result.append("/OperationNames");
String responseHtml = result.toString();
response.setCharacterEncoding( "UTF-8");
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(responseHtml);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
rs.close();
st.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}

/** *//**
* 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
{
this.doGet(request,response);
}

/** *//**
* Initialization of the servlet. br
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException
{
// Put your code here
}

}
index.jsp

ajax("servlet/OperationName?names=" + newOperationName + "&form=form1");"&form=form1"可去掉

%@ page language="java" contentType="text/html;charset=GBK"%
%
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%"

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

titleMy JSP 'MyJsp.jsp' starting page/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"
--
Script language="javascript"
!--
function ajax(url){
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
xmlHttp.onreadystatechange = processResponse;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function processResponse(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
if(xmlHttp.status==200)
{

var result = xmlHttp.responseXML;
var operationName = result.getElementsByTagName("OperationName");
if(operationName[0].firstChild.nodeValue == "0"){
document.all.ShowInfo.innerHTML = "B不存在B";
}else{
//alert(22222);
document.all.ShowInfo.innerHTML = "B存在B";
}
//document.all.ShowInfo.innerHTML = "font color="red"*/font";

}
return true;
}
}
function aaa()
{
var newOperationName = document.all.names.value;
ajax("servlet/OperationName?names=" + newOperationName + "&form=form1");
}
--
/Script
/head

body
form action="index.jsp"
table
tr
td
input type="text" name="names" value="" Onblur="aaa()"
/td
/tr
tr
TD bgcolor="#EEEEEE" id="ShowInfo"
asdas
/TD
/tr
tr
!--input type="button" onclick="aaa();" value="提交"--
/tr
/table
/form
/body
/html

web.xml

?xml version="1.0" encoding="UTF-8"?
web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
servlet
servlet-nameOperationName/servlet-name
servlet-classservlet.OperationName/servlet-class
/servlet

servlet-mapping
servlet-nameOperationName/servlet-name
url-pattern/servlet/OperationName/url-pattern
/servlet-mapping
/web-app

刚刚接触ajax不久,如有不合理的地方还望指点,谢谢!

来源:https://www.tulaoshi.com/n/20160219/1613659.html

延伸阅读
标签: Web开发
首先在项目里面添加服务引用 ---------------------------验证 Email 地址是否正确.aspx----------------- 代码如下: html xmlns="http://www.w3.org/1999/xhtml" head runat="server" title/title /head body form id="form1" runat="server" asp:ScriptManager ID="ScriptManager1" runat="server" /asp:ScriptManager asp:Updat...
标签: Web开发
验证注册用户名必须为字母,或者字母+数字,或者全中文,字母是3-20个字符,中文是3-10个 [Ctrl+A 全选]
标签: Web开发
1.建立一个aspx页面 html代码 html xmlns="http://www.w3.org/1999/xhtml" head id="Head1" runat="server"     title小山/title     link type="text/css" href="../../Styles/tree_css/tree.css" rel="stylesheet" /head body     form id="Form1" runat...
标签: 应用软件
输入帐号 在淘宝官网中输入正确的帐号以及密码,点击登陆。 进入淘宝 登陆完成后,点击左上角的“我的淘宝”。 点击昵称 进入之后你会看到下图,在你的淘宝昵称上点击一下。 编辑资料 这时候会打开资料编辑页面,在里面找到昵称并在后面文本框中输入你想改的名字。 选择保存 填写好信息之后,点击...
标签: Web开发
我们在做验证码的时候往往由于要反作弊,验证有时故意加入多的干扰因素,这时验证码显示不很清楚,用户经常输入错误。这样不但要重新刷新页面,导致用户没有看清楚验证码而重填而不是修改,而且如果没有用session保存下用户输入的其它数据的话(如姓名),用户刚刚输入的内容也不存在了,这样给用户造成不好的体验。 本例在原有验证方式基础之上增...

经验教程

752

收藏

21
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部