这个是我在网上看到的一个有错的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>
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 三国志8重制版恶名怎么消除 恶名影响与消除方法介绍
- 模拟之声慢刻CD《柏林之声5》2019[原抓WAV+CUE]
- AlexandraSoumm-Parisestunefte(2024)[24Bit-96kHz]FLAC
- 李嘉《国语转调1》[天王唱片][WAV整轨]
- 不是哥们 这都能跑?网友展示用720显卡跑《黑神话》
- 玩家自制《黑神话:悟空》亢金星君3D动画 现代妆容绝美
- 大佬的审美冲击!《GTA6》环境设计师展示最新作品
- 纪晓君.2001-野火·春风【魔岩】【WAV+CUE】
- 汪峰.2005-怒放的生命【创盟音乐】【WAV+CUE】
- 群星.1995-坠入情网【宝丽金】【WAV+CUE】
- 群星《谁杀死了Hi-Fi音乐》涂鸦精品 [WAV+CUE][1G]
- 群星1998《宝丽金最精彩98》香港首版[WAV+CUE][1G]
- 汪峰《也许我可以无视死亡》星文[WAV+CUE][1G]
- 李嘉-1991《国语转调2》[天王唱片][WAV整轨]
- 蔡琴2008《金声回忆录101》6CD[环星唱片][WAV整轨]