AJAX如何判定用户是否注册
这篇文章主要介绍AJAX如何判定用户是否注册,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
目前创新互联已为近千家的企业提供了网站建设、域名、网络空间、网站托管、企业网站设计、北林网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
在很多注册的页面上,我们可能会遇到以下情况,当我们注册用户名的时候可能会提示该用户名已注册,其实现是应用AJAX技术。
首先写一个登陆页面
随后写一个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中进行配置
index.jsp CheckServlet CheckServlet CheckServlet /CheckServlet
以上是“AJAX如何判定用户是否注册”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
文章名称:AJAX如何判定用户是否注册
地址分享:http://pwwzsj.com/article/gccigc.html