AJAX,JSON用户名校验
效果
开发结构
1,src部分有两个包dao和servlet
1.1dao包下有两个数据库工具类文件
SqlHelper.java
package org.guangsoft.dao; import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author guanghe
*/
public class SqlHelper
{
//定义连接资源
private static Connection ct = null;
private static PreparedStatement ps = null;
private static CallableStatement cs = null;
private static ResultSet rs = null; //定义配置参数
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null; private static Properties pp = null;
private static InputStream is = null; //读取配置参数,加载驱动
static
{
try
{
pp = new Properties();
is = SqlHelper.class.getClassLoader().getResourceAsStream("org/guangsoft/dao/db.properties");
pp.load(is);
driver = pp.getProperty("driver");
url = pp.getProperty("url");
username = pp.getProperty("username");
password = pp.getProperty("password");
Class.forName(driver);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
finally
{
try
{
is.close();
}
catch (Exception e)
{
e.printStackTrace();
}
is = null;
}
} //获取连接
public static Connection getConnection()
{
try
{
ct = DriverManager.getConnection(url, username, password);
}
catch (Exception e)
{
e.printStackTrace();
}
return ct;
} public static PreparedStatement getPs()
{
return ps;
} public static ResultSet getRs()
{
return rs;
} //执行DQL查询
public static ResultSet executeQuery(String sql, String[] parameters)
{
try
{
ct = getConnection();
ps = ct.prepareStatement(sql);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
ps.setString(i + 1, parameters[i]);
}
}
rs = ps.executeQuery();
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return rs;
} //执行DML更新
public static int executeUpdate(String sql, String[] parameters)
{
try
{
ct = getConnection();
ps = ct.prepareStatement(sql);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
ps.setString(i + 1, parameters[i]);
}
}
return ps.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally
{
close(ct, ps, rs);
}
} //执行DML批处理
public static int executeUpdate2(String[] sql, String[][] parameters)
{
try
{
ct = getConnection();
ct.setAutoCommit(false); for (int i = 0; i < sql.length; i++)
{ if (null != parameters[i])
{
ps = ct.prepareStatement(sql[i]);
for (int j = 0; j < parameters[i].length; j++)
{
ps.setString(j + 1, parameters[i][j]);
}
return ps.executeUpdate();
} }
ct.commit();
}
catch (Exception e)
{
e.printStackTrace();
try
{
ct.rollback();
}
catch (Exception e1)
{
e1.printStackTrace();
}
throw new RuntimeException(e.getMessage());
}
finally
{
close(ct, ps, rs);
}
return 0;
} //执行存储过程
public static CallableStatement callPro1(String sql, String[] parameters)
{
try
{
ct = getConnection();
cs = ct.prepareCall(sql);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
cs.setObject(i + 1, parameters[i]);
}
}
cs.execute();
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally
{
close(ct, cs, rs);
}
return cs;
} //执行高级存储过程
public static CallableStatement callPro2(String sql, String[] inparameters,
Integer[] outparameters)
{
try
{
ct = getConnection();
cs = ct.prepareCall(sql);
if (inparameters != null)
{
for (int i = 0; i < inparameters.length; i++)
{
cs.setObject(i + 1, inparameters[i]);
}
}
if (outparameters != null)
{
for (int i = 0; i < outparameters.length; i++)
{
cs.registerOutParameter(inparameters.length + 1 + i, outparameters[i]);
}
}
cs.execute();
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally
{
close(ct, cs, rs);
}
return cs;
} //关闭所有资源连接
public static void close(Connection ct, Statement ps, ResultSet rs)
{
if (rs != null)
{
try
{
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
rs = null;
}
if (ps != null)
{
try
{
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
ps = null;
}
if (null != ct)
{
try
{
ct.close();
}
catch (Exception e)
{
e.printStackTrace();
}
ct = null;
}
}
}
db.properties
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/student
username = root
password =root
1.2servlet包下有一个文件Servlet
CheckUname.java
package org.guangsoft.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.guangsoft.dao.SqlHelper; public class CheckUname extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
} protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet resultSet = null;
try
{
connection = SqlHelper.getConnection();
String sql = "select * from user where username=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1,username);
resultSet = prepareStatement.executeQuery();
while(resultSet.next())
{
response.getWriter().print("true");
return;
}
response.getWriter().print("false");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
SqlHelper.close(connection, prepareStatement, resultSet);
}
} }
2,webRoot部分有两个文件js工具类和jsp页面
2.1 js工具类封装了操作json数据的函数
AjaxUtil.js
function sendAjaxReq(method,url,param,fun200,fun404,fun500)
{
var req;
if(window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
req.open(method,url);
req.onreadystatechange = function()
{
if(req.readyState == 4)
{
if(req.status == 200)
{
if(fun200)
{
fun200(req.responseText);
}
}
else if(req.status == 404)
{
if(fun404)
{
fun404();
}
}
else if(req.status == 500)
{
if(fun500)
{
fun500();
}
}
}
}
if(method.toUpperCase() == "GET")
{
req.send(null);
}
else if(method.toUpperCase() == "POST")
{
req.setRequestHeader("context-type", "application/x-www-form-urlencoded");
req.send(param);
}
}
2.2 jsp页面是显示页面
index.jsp
<%@ 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%>">
<style>
.success
{
color:green;
}
.fail
{
color:red;
}
</style>
<script src="/ajax/js/AjaxUtil.js"></script>
<script>
function checkUname(unameValue)
{
if(unameValue != "")
{
sendAjaxReq("get","checkUname?username="+unameValue,null,function(data)
{
if(eval(data))
{
document.getElementById("unameSpan").innerHTML="该用户名已被注册!";
document.getElementById("unameSpan").className="fail";
}
else
{
document.getElementById("unameSpan").innerHTML="恭喜,可以注册!";
document.getElementById("unameSpan").className="success";
}
});
}
} </script>
</head> <body>
<form method="get" action="/ajax/checkUname">
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" name="username" id="username"
onblur="checkUname(this.value);"
onkeypress="document.getElementById('unameSpan').innerHTML='';" />
</td>
<td><span name="unameSpan" id="unameSpan"></span></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" /></td>
</tr>
</table>
<input type="submit" value="提交">
</form> </body>
</html>
AJAX,JSON用户名校验的更多相关文章
- ajax实现用户名校验的传统和jquery的$.post方式
第一种:传统的ajax异步请求,后台代码以及效果在最下边 首先我们在eclipse中创建一个注册页面regist.jsp,创建一个form表单,注意,由于我们只是实现用户名校验的效果,下边红色部门是我 ...
- AJAX,JSON搜索智能提示
效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...
- 案例1.用Ajax实现用户名的校验
用Ajax实现用户名的校验 java的验证类 public class UserDao { public boolean checkUserName(String name) { //这里的name是 ...
- 使用AJAX完成用户名是否存在异步校验
一.JSP代码: 1.事件触发:onblur 2.编写AJAX代码:向Action中提交,传递username参数 <script> function checkUsername(){ / ...
- thinkphp中ajax用户名校验
ajax实在是太神奇了,刚刚接触,不足之处,请大家指正. 采用Ajax方式进行页面无刷新提示,来检测用户名是否存在. 搭建一个thinkphp的环境,在index.html中,ajax代码如下: &l ...
- ajax用户名校验demo详解
//用户名校验的方法 //这个方法使用XMLHTTPRequest对象进行AJAX的异步数据交互 var xmlhttp; function verify(){ //1.使用dom的方式获取文本框中的 ...
- SSH网上商城---使用ajax完成用户名是否存在异步校验
小伙伴在上网的时候,需要下载或者观看某些视频资料,更或者是在逛淘宝的时候,我们都需要注册一个用户,当我们填写好各种信息,点击确定的时候,提示用户名已经存在,小编就想,为什么当我们填写完用户名的时候,她 ...
- Ajax (Asynchronous javascript xml) 搜索框核心代码(JQuery) Ajax判断用户名存在核心代码 附:原生js的Ajax代码 其中有json的一句话解释
前端 <script type="text/javascript"> $(function(){ $("#tid").keyup(function( ...
- ajax案例_校验用户名
目录 ajax案例_校验用户名 代码下载 需求 流程 搭建环境 开发代码 1_jsp 1_servlet 1_service.dao 2_servlet 2_jsp 测试后,功能实现,完结撒花 aja ...
随机推荐
- PHP 数据库驱动、连接数据不同方式学习笔记
相关学习资料 http://www.php.net/manual/zh/refs.database.php http://www.php.net/manual/zh/internals2.pdo.ph ...
- OracleOraDb10g_home1TNSListener无法启动
1:“本地计算机上的OracleOraDb10g_home1TNSListener服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 解决办法:动态ip,服务 OracleOraDb10 ...
- groovy-位运算
从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程
1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...
- jquery------添加jQuery对象方法
my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...
- ECSHOP验证码背景图修改教程
ECSHOP验证码背景图修改教程 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-18 ECSHOP验证码背景图修改教程: ECSHOP前后台的某些地 ...
- js校验表单后提交表单的三种方法总结
第一种: 复制代码 代码如下: <script type="text/javascript"> function check(form) { if(fo ...
- WGS84、Web墨卡托、火星坐标、百度坐标互转
转自:1.http://blog.csdn.net/wildboy2001/article/details/12031351 2.http://kongxz.com/2013/10/wgs-cgj/ ...
- The mean shift clustering algorithm
The mean shift clustering algorithm MEAN SHIFT CLUSTERING Mean shift clustering is a general non-par ...