這篇文章主要介紹AJAX如何判定用戶是否注冊,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、景洪網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、購物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為景洪等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。在很多注冊的頁面上,我們可能會遇到以下情況,當(dāng)我們注冊用戶名的時候可能會提示該用戶名已注冊,其實現(xiàn)是應(yīng)用AJAX技術(shù)。
首先寫一個登陸頁面
隨后寫一個servlet的Java代碼
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CheckServlet extends HttpServlet{ public static final String DBDRIVER = "oracle.jdbc.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin:@59.173.240.149:1521:heer"; public static final String DBUSER = "hnsyu_dev"; public static final String DBPASS = "hnsyuok"; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ this.doPost(request, response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("gbk"); response.setContentType("text/html"); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; PrintWriter out = response.getWriter(); String userid = request.getParameter("userid"); try { Class.forName(DBDRIVER); connection = DriverManager.getConnection(DBURL, DBUSER, DBPASS); String sql = "select count(userid) from userdemo where userid=?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,userid); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { if(resultSet.getInt(1)>0){ out.print("false"); System.out.println("true"); }else { out.print("false"); } } out.close(); } catch (Exception e) { e.printStackTrace(); }finally{ try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } } }
其中還需要在web.xml中進(jìn)行配置
http://weahome.cn/article/dodgde.html