在java Web中如何用Ajax实现用户名已存在
我是初学者,请高手提供代码,包括java类,和jsp页面的代码,急啊~~~~!!
我是初学者,请高手提供代码,包括java类,和jsp页面的代码,急啊~~~~!!
答案:数据库操作类(mysql数据库)1.import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class BaseDao{
protected Connection conn;
protected Statement sta;
protected ResultSet rs;
protected PreparedStatement ps;
public void setConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dianlv";
conn = DriverManager.getConnection(url,"root","root");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeAll(){
try{
if(rs!=null){
rs.close();
}if(ps!=null){
ps.close();
}if(sta!=null){
sta.close();
}if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
2.UserDao类集成baseDao类,实现对数据库中的数据名字超找:import java.sql.SQLException;
public class UserDao extends BaseDao {
/**
* 验证用户名是否成功;如果成功了返回true;
*
* **/
public boolean checkUserName(String userName){
boolean flag = false;
this.setConnection();
String sql ="select * from users where userName = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,userName);
rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}3.上面代码实现应该有一个user实体:创建user实体类:
public class User {
private String userName;
private int id;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.写一个servlet用户验证用户名是否重复;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.fz.dao.UserDao;
public class CheckUserNameServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doPost(request, response);
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
5:jsp页面用于用户的操作,上面用到ajax技术用户验证用户名是否重复<%@ 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=>
<title>My JSP 'Login.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">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function checkUnique(){
var content = $('#userName').val(); //获取文本框中输入的内容
var url = 'servlet/CheckUserNameServlet';
$.post(
url, //处理请求的url
{'name':content}, //传递的参数
function(data){ //请求处理完毕之后的回调函数
if(data == 'true'){//返回的是字符串不能用data = false这样比较
$("#s1").text("用户名不唯一");```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
}else if(data == 'false'){
$("#s1").text("用户名可用");
}
}
);
}
</script></head>
<body>
<% request.setCharacterEncoding("utf-8");
%>
<form action="">
用户名:<input type="text" name="uerName" onblur="checkUnique();" id = "userName"/> <span id="s1"></span><br/>
密码:<input type="pas