欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ajax检测用户名是否已经注册

程序员文章站 2022-07-13 16:54:45
...

程序功能
当用户名输入完成(即用户名输入框失去焦点),把数据传递给MySQL数据库,利用Ajax检测用户名是否已经注册!
服务器:tomcat 数据库:MySQL

HTML部分

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--     uName uPwd传到后台 -->
		username:<input type="text" id="uName"/><br/>
		pwd:<input type="password" id="uPwd" /><br/>
		<button id="loginBtn">点击登录</button><span id="loginMsg"></span>
		<script type="text/javascript" src="js/jquery/jquery-1.7.2.min.js"></script>
		<script type="text/javascript">
			$("#loginBtn").click(function(){
				var uName=$("#uName").html()
				var uPwd=$("#uPwd").html()
				
				$.ajax({
					   type: "POST",
					   url: "login",
					   data: "uName="+uName+"&uPwd="+uPwd,
					   success: function(data){
						   var str =$.parseJSON(data);
					     	if(data=='3'){
					     		$("#loginMsg").text("账户/密码错误 请重试")
					     		alert("3");
					     	}else if(data=='0'){
					     		$("#loginMsg").text("账户被禁用,请联系管理员")
					     	}
					     	else if(data=='1'){
					     		window.location.href='main.html'
					     	}
					   }
					});
			})
			
		</script>
</body>
</html>

XML配置部分

<!--  login配置文件 -->
  <servlet>
  	<servlet-name>loginServlet</servlet-name>
  	<servlet-class>com.gezhi.controller.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>loginServlet</servlet-name>
  	<url-pattern>/login</url-pattern>
  </servlet-mapping>

Dao部分

package com.gezhi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.gezhi.pojo.User;
import com.gezhi.pojo.UserInfo;
import com.gezhi.util.JDBCUtil;

public class LoginDao {
  public  User getUser(String uName ,String uPwd) throws Exception{
	  
	  
	  Connection conn = JDBCUtil.getCon();
	  
	  StringBuilder sb = new StringBuilder();
	  
		sb.append("select user_name, user_pwd, user_id ,user_type from users ");
		sb.append(" WHERE user_name=? AND user_pwd=? ");
		
		PreparedStatement ptmt = conn.prepareStatement(sb.toString());
		ptmt.setString(1, uName);
		ptmt.setString(2, uPwd);
		User users = new User();
		ResultSet rs= ptmt.executeQuery();
		if(rs.next()){
			
			int userId=rs.getInt("user_id");
			String userName=rs.getString("user_name");
			String userPwd = rs.getString("user_pwd");
			int userType=rs.getInt("user_type");
			
			users.setUserName(userName);
			users.setUserPwd(userPwd);
			users.setUserType(userType);
			
			System.out.println("找到了");
		}
		else {
			System.out.println("没找到");
		}
		JDBCUtil.closeAll(rs, ptmt, conn);
		
	  return users;
  }
}

servlet部分

package com.gezhi.controller;
/**
 * @功能 登录程序
 */
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 javax.servlet.http.HttpSession;

import com.gezhi.dao.LoginDao;
import com.gezhi.pojo.User;



public class LoginServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		PrintWriter out = resp.getWriter();
		
		String name =req.getParameter("uName");
		String pwd = req.getParameter("uPwd");
		String type = req.getParameter("type");
		//调用Dao层的  loginDao 类中的 方法  拿到它的返回值
		LoginDao dao = new LoginDao();
		
		try {
//			System.out.println(1); //           flag 
			 User user = dao.getUser(name, pwd);
//			System.out.println(2); //           flag 
			//对返回值进行判断  如果不是空  就说明找到了  就像前端发送  1 
			 if (user!=null&&user.getUserType()!=null) {
	
				 if(user.getUserType()==0){
						//禁止登录
						out.print("0");
					}else if(user.getUserType()==1){
						//登录成功
						out.print("1");
						//保存用户状态
						HttpSession session=req.getSession();
						//session.setMaxInactiveInterval(2*60*60);
						session.setAttribute("user", user);
						System.out.println(session.getMaxInactiveInterval());
					}
				}
			 //否则就是账号或者密码有问题
			 else if(user==null){
					//帐户名/密码错误
					out.print("3");
				}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print("3");
		}
		 
		 		
}
}

JDBC工具类

package com.gezhi.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 工具层
 * @author Administrator
 *
 */
public class JDBCUtil {
	public static final String URL = "jdbc:mysql://localhost:3306/tb40";
	public static final String USER = "root";
	public static final String PASSWORD = "123456";
	 static {
		 try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 /**
	  * 创建数据库连接
	  */
	 public static Connection getCon() {
		 Connection conn = null;
		 
		 try {
			conn = DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return conn;
	 }
	
		/**
		 * 关闭结果集,关闭预处理平台,关闭数据库连接
		 * @param rs
		 * @param prepare
		 * @param conn
		 */
		public static void closeAll(ResultSet rs,PreparedStatement prepare,Connection conn){
			if(rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(prepare != null) {
				try {
					prepare.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
}

相关标签: 注册