【DRP】AJAX异步判断用户是否存在
用户名 和提示:
1 <td width="78%"> 2 <input name="userId" type="text" class="text1" id="userId" 3 size="10" maxlength="10" onkeypress="userIdOnKeyPress()" value="<%=userId %>" onblur="validate(this)"> 4 <span id="spanUserId"></span> 5 </td>
失去焦点的事件:
function validate(field) { //alert(document.getElementById("userId").value); //alert(field.value); if (trim(field.value).length != 0) { //创建Ajax核心对象XMLHttpRequest createXMLHttpRequest(); var url = "user_validate.jsp?userId=" + trim(field.value) + "&time=" + new Date().getTime(); //设置请求方式为GET,设置请求的URL,设置为异步提交 xmlHttp.open("GET", url, true); //将方法地址复制给onreadystatechange属性 //类似于电话号码 xmlHttp.onreadystatechange=callback; //将设置信息发送到Ajax引擎 xmlHttp.send(null); } else { document.getElementById("spanUserId").innerHTML = ""; } }
验证AJAX是否请求成功:
function callback() { //alert(xmlHttp.readyState); //Ajax引擎状态为成功 if (xmlHttp.readyState == 4) { //HTTP协议状态为成功 if (xmlHttp.status == 200) { if (trim(xmlHttp.responseText) != "") { //通过span标签 提示用户信息 document.getElementById("spanUserId").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>" }else { document.getElementById("spanUserId").innerHTML = ""; } }else { alert("请求失败,错误码=" + xmlHttp.status); } } }
接收 GET 请求传过来的值:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="com.bjpowernode.drp.sysmgr.manager.*" %> <% //Thread.currentThread().sleep(5000); String userId = request.getParameter("userId"); if (UserManager.getInstance().findUserById(userId) != null) { out.println("用户代码已经存在"); } %>
调用底层方法查询数据库:
/** * 根据用户代码查询 * @param userId * @return 如果存在返回User对象,否则返回null */ public User findUserById(String userId) { String sql = "select user_id, user_name, password, contact_tel, email, create_date from t_user where user_id=?"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; User user = null; try { conn = DbUtil.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, userId); rs = pstmt.executeQuery(); if (rs.next()) { user = new User(); user.setUserId(rs.getString("user_id")); user.setUserName(rs.getString("user_name")); user.setPassword(rs.getString("password")); user.setContactTel(rs.getString("contact_tel")); user.setEmail(rs.getString("email")); user.setCreateDate(rs.getTimestamp("create_date")); } }catch(SQLException e) { e.printStackTrace(); }finally { DbUtil.close(rs); DbUtil.close(pstmt); DbUtil.close(conn); } return user; }
连接数据库:
public static Connection getConnection() {
//如果不用XML文件 可以直接用下面注释掉的代码。 // Connection conn = null; // try { // Class.forName("oracle.jdbc.driver.OracleDriver"); // String url = "jdbc:oracle:thin:@localhost:1521:bjpowern"; // String username = "drp1"; // String password = "drp1"; // conn = DriverManager.getConnection(url, username, password); // } catch (ClassNotFoundException e) { // e.printStackTrace(); // } catch (SQLException e) { // e.printStackTrace(); // } // return conn;
//读取XML文件中的连接数据库信息
Connection conn = null; try { JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig(); Class.forName(jdbcConfig.getDriverName()); conn = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(), jdbcConfig.getPassword()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
总结:让以后的开发变的高效起来!