真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

實現(xiàn)AJAX+JAVA用戶登陸注冊驗證的代碼

【相關(guān)學(xué)習(xí)推薦:java基礎(chǔ)教程】

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的大埔網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

需求

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

技術(shù)棧

JSP+Servlet+Oracle

具體代碼

JSP部分:

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




Insert title here



  請輸入用戶名:
  

這里沒有用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();
    }
  }
}

關(guān)于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;
    }

關(guān)于控制層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);//調(diào)用service中的查詢方法
    response.setCharacterEncoding("UTF-8");//設(shè)置字符編碼
    PrintWriter out = response.getWriter();
    out.print(isUname);
    out.flush();
    out.close();//關(guān)閉資源
  }

  /**
   * @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);
  }
}

相關(guān)文章推薦:ajax視頻教程


分享名稱:實現(xiàn)AJAX+JAVA用戶登陸注冊驗證的代碼
URL地址:http://weahome.cn/article/cggcdo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部