中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

實現AJAX+JAVA用戶登陸注冊驗證的方法是什么

發布時間:2020-08-26 10:09:11 來源:億速云 閱讀:206 作者:小新 欄目:編程語言

小編給大家分享一下實現AJAX+JAVA用戶登陸注冊驗證的方法是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

需求

通過ajax異步刷新頁面驗證用戶輸入的賬號密碼是否在數據庫中存在。

技術棧

JSP+Servlet+Oracle

具體代碼

JSP部分:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script>
  function createXMLHttpRequest() {
    try {
      xmlHttp = new XMLHttpRequest();//除了ie之外的其他瀏覽器使用ajax
    } catch (tryMS) {
      try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");//ie瀏覽器適配
      } catch (otherMS) {
        try {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie瀏覽器適配
        } catch (failed) {
          xmlHttp = null;
        }
      }
    }
    return xmlHttp;
  }
  //提交請求
  var xmlHttp;
  function checkUserExists() {
    var u = document.getElementById("uname");
    var username = u.value;
    if (username == "") {
      alert("請輸入用戶名");
      u.focus();
      return false;
    }
    //訪問字符串
    var url = "loginServlet";
    //創建核心xmlhttprequest組件
    xmlHttp = createXMLHttpRequest();
    //設置回調函數
    xmlHttp.onreadystatechange = proessRequest;
    //初始化核心組件
    xmlHttp.open("post", url, true);
    //設置請求頭
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    //發送請求
    xmlHttp.send("uname="+username);
  }
  //回調函數
  function proessRequest() {
    if (xmlHttp.status==200 && xmlHttp.readyState == 4) {
      var b = xmlHttp.responseText;//得到服務端的輸出結果
      if (b=="true") {
        document.getElementById("alert").innerHTML = "<font color='red'>用戶名已經存在!</font>";
      }else {
        document.getElementById("alert").innerHTML = "<font color='blue'>用戶名可以使用!</font>";
      }
    }
  }
</script>
<body>
  請輸入用戶名:
  <input id="uname" name="uname" type="text" onblur="checkUserExists()" /><p id="alert" style="display:inline"></p>
</body>
</html>

這里沒有用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='"+uname+"'";//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+JAVA用戶登陸注冊驗證的方法是什么的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

竹山县| 兴海县| 邓州市| 昌江| 石河子市| 韶山市| 崇仁县| 集贤县| 手游| 尼木县| 图们市| 紫云| 巴彦县| 浦县| 宾川县| 柳河县| 嘉兴市| 夏邑县| 富顺县| 岑巩县| 曲松县| 忻城县| 麻江县| 巧家县| 乌海市| 五华县| 隆尧县| 和林格尔县| 华宁县| 应城市| 平定县| 朝阳区| 四平市| 阿克陶县| 孝昌县| 青龙| 澎湖县| 常德市| 呼图壁县| 闻喜县| 宁晋县|