热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

实现AJAX+JAVA用户登陆注册验证的代码

这篇文章主要介绍了AJAX+JAVA用户登陆注册验证的实现代码,通过ajax异步刷新页面验证用户输入的账号密码是否在数据库中存在。

JSP部分:

<%@ page language="java" cOntentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>








  请输入用户名:
  

这里没有用Dao层,直接用servlet和service层进行验证。

下面是service下JDBC查询的代码:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.stx.service.User;
import com.stx.service.ConnectionManager;

public class ajaxService {
  public boolean searchUser (String uname) {
  //jdbc查询用户名是否存在
    boolean isFalse = false;
    Connection cOnnection= null;
    Statement stmt = null;
    ResultSet rs = null;
    cOnnection= ConnectionManager.getConnection();
    try {
      stmt = connection.createStatement();
      String sql = "select * from user_b where uname=&#39;"+uname+"&#39;";//sql语句
      rs = stmt.executeQuery(sql);
      isFalse=rs.next();

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      ConnectionManager.closeResultSet(rs);
      ConnectionManager.closeStatement(stmt);
      ConnectionManager.closeConnection(connection);
    }
    return isFalse;
  }
}

JDBC连接代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ConnectionManager {
  private final static String DRIVER_CLASS = "oracle.jdbc.OracleDriver";
  private final static String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
  private final static String DBNAME = "ibook";
  private final static String PASSWORD = "qwer";

  public static Connection getConnection() {
    Connection cOnnection= null;
    try {
      Class.forName(DRIVER_CLASS);
      cOnnection= DriverManager.getConnection(URL, DBNAME, PASSWORD);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return connection;
  }

  public static void closeResultSet(ResultSet rs) {
    try {
      if (rs != null)
        rs.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeConnection(Connection connection) {
    try {
      if (connection != null && !connection.isClosed())
        connection.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeStatement(Statement stmt) {
    try {
      if (stmt != null)
        stmt.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

关于user类:

 public class User {
    private String uname;
    public User() {
      super();
    }
    public User(String uname) {
      super();
      this.uname = uname;
  
    }
  
    public String getUname() {
      return uname;
    }
    public void setUname(String uname) {
      this.uname = uname;
    }

关于控制层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.stx.service.ajaxService;

/**
 * Servlet implementation class loginServlet
 */
public class loginServlet extends HttpServlet {
  private static final long serialVersiOnUID= 1L;
  private ajaxService ajaxService = new ajaxService();

  /**
   * @see HttpServlet#HttpServlet()
   */
  public loginServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String uname = request.getParameter("uname");//获取到输入的用户名
    boolean isUname = ajaxService.searchUser(uname);//调用service中的查询方法
    response.setCharacterEncoding("UTF-8");//设置字符编码
    PrintWriter out = response.getWriter();
    out.print(isUname);
    out.flush();
    out.close();//关闭资源
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
  }
}

相关文章推荐:ajax视频教程

以上就是实现AJAX+JAVA用户登陆注册验证的代码的详细内容,更多请关注其它相关文章!


推荐阅读
author-avatar
dvs5453401
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有