无为清净楼资源网 Design By www.qnjia.com
这个是我在网上看到的一个有错的ajax 我改正确了,却找不到那个帖子了。
用servlet验证:
复制代码 代码如下:
package Servlet;
import java.io.*;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class CheckServlet extends HttpServlet {
static HttpServletRequest request = null;
static HttpServletResponse response = null;
public static void main(String[] args) throws Exception, IOException{
CheckServlet c = new CheckServlet();
c.doPost(request, response);
}
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/spring";
public static final String DBUSER = "root";
public static final String DBPASS = "liangke";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");// 接受验证的用户名
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = " select * from user where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
StringBuffer jsonStr=new StringBuffer();
jsonStr.append("[{flag:'");
if(rs.next()){
jsonStr.append("true',alertTest:'aaa'}]");
out.println(jsonStr.toString());
}else{
jsonStr.append("false',alertTest:'bbb'}]");
out.println(jsonStr.toString());
}
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
前台显示页面:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!doctype html>
<html lang="en">
<body>
<script>
//onload 当页面加载的时候会调用这个方法initPage
window.onload = initPage;
function initPage() {
alert("ee");
document.getElementById("userid").onblur = checkUserid;//引用函数方法。 onblur 事件在用户离开输入框时执行 JavaScript 代码
}
//创建请求对象
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
//检查用户名是否重复
function checkUserid(){
alert("dd");
request=createRequest();
var theName = document.getElementById("userid").value;
request.open("POST","CheckServlet?userid="+theName,true);
request.onreadystatechange=checkUseridCallback;
request.send(null);
document.getElementById("msg").innerHTML="正在验证...";
}
//回调函数
function checkUseridCallback(){
if(request.readyState==4){
if(request.status==200){
var v = request.responseText;
var value1= eval(v)[0];
if( value1.flag=="true"){
document.getElementById("msg").innerHTML="用户ID重复";
}else{
document.getElementById("msg").innerHTML="此用户ID可以注册";
}
}
}
}
</script>
<form action="check.jsp" method="POST">
用户ID:
<input type="text" id="userid" name="userid">
<span id="msg"></span>
<br>
姓名
<input type="text" name="name">
</form>
</body>
</html>
用servlet验证:
复制代码 代码如下:
package Servlet;
import java.io.*;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class CheckServlet extends HttpServlet {
static HttpServletRequest request = null;
static HttpServletResponse response = null;
public static void main(String[] args) throws Exception, IOException{
CheckServlet c = new CheckServlet();
c.doPost(request, response);
}
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/spring";
public static final String DBUSER = "root";
public static final String DBPASS = "liangke";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");// 接受验证的用户名
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = " select * from user where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
StringBuffer jsonStr=new StringBuffer();
jsonStr.append("[{flag:'");
if(rs.next()){
jsonStr.append("true',alertTest:'aaa'}]");
out.println(jsonStr.toString());
}else{
jsonStr.append("false',alertTest:'bbb'}]");
out.println(jsonStr.toString());
}
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
前台显示页面:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!doctype html>
<html lang="en">
<body>
<script>
//onload 当页面加载的时候会调用这个方法initPage
window.onload = initPage;
function initPage() {
alert("ee");
document.getElementById("userid").onblur = checkUserid;//引用函数方法。 onblur 事件在用户离开输入框时执行 JavaScript 代码
}
//创建请求对象
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
//检查用户名是否重复
function checkUserid(){
alert("dd");
request=createRequest();
var theName = document.getElementById("userid").value;
request.open("POST","CheckServlet?userid="+theName,true);
request.onreadystatechange=checkUseridCallback;
request.send(null);
document.getElementById("msg").innerHTML="正在验证...";
}
//回调函数
function checkUseridCallback(){
if(request.readyState==4){
if(request.status==200){
var v = request.responseText;
var value1= eval(v)[0];
if( value1.flag=="true"){
document.getElementById("msg").innerHTML="用户ID重复";
}else{
document.getElementById("msg").innerHTML="此用户ID可以注册";
}
}
}
}
</script>
<form action="check.jsp" method="POST">
用户ID:
<input type="text" id="userid" name="userid">
<span id="msg"></span>
<br>
姓名
<input type="text" name="name">
</form>
</body>
</html>
标签:
ajax,注册名,验证
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月11日
2024年11月11日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]