AJAX用于判定用户是否注册
在很多注册的页面上,我们可能会遇到以下情况,当我们注册用户名的时候可能会提示该用户名已注册,其实现是应用AJAX技术。
首先写一个登陆页面
<html> <head> <title></title> <scripttype="text/javascript"> varxmlHttp; varflag; functioncreateXMLHttp(){ if(window.XMLHttpRequest){ xmlHttp=newXMLHttpRequest(); }else{ xmlHttp=newActiveXObject("Microsoft.XMLHTTP"); } } functioncheckUserid(userid){ createXMLHttp(); xmlHttp.open("POST","CheckServlet?userid="+userid); xmlHttp.onreadystatechange=checkUseridCallback; xmlHttp.send(); document.getElementById("msg").innerHTML="正在验证。。。"; } functioncheckUseridCallback(){ if(xmlHttp.readyState==400){ if(xmlHttp.status==200){ vartext=xmlHttp.responseText; if(text=="true"){ flag=false; document.getElementById("msg").innerHTML="用户ID重复,无法使用"; }else{ flag=true; document.getElementById("msg").innerHTML="此用户ID可以注册"; } } } } functioncheckForm(){ returnflag; } </script> </head> <body> <formaction="tt.jsp"method="post"onsubmit="returncheckForm()"> 用户ID<inputtype="text"name="userid"onblur="checkUserid(this.value)"><spanid="msg"></span><br> 姓名:<inputtype="text"name="name"><br> 密码:<inputtype="password"name="password"><br> <inputtype="button"value="注册"> <inputtype="reset"value="重置"> </form> </body> </html>
随后写一个servlet的Java代码
importjava.io.IOException; importjava.io.PrintWriter; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.PreparedStatement; importjava.sql.ResultSet; importjava.sql.SQLException; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; publicclassCheckServletextendsHttpServlet{ publicstaticfinalStringDBDRIVER="oracle.jdbc.OracleDriver"; publicstaticfinalStringDBURL="jdbc:oracle:thin:@59.173.240.149:1521:heer"; publicstaticfinalStringDBUSER="hnsyu_dev"; publicstaticfinalStringDBPASS="hnsyuok"; publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ this.doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ request.setCharacterEncoding("gbk"); response.setContentType("text/html"); Connectionconnection=null; PreparedStatementpreparedStatement=null; ResultSetresultSet=null; PrintWriterout=response.getWriter(); Stringuserid=request.getParameter("userid"); try{ Class.forName(DBDRIVER); connection=DriverManager.getConnection(DBURL,DBUSER,DBPASS); Stringsql="selectcount(userid)fromuserdemowhereuserid=?"; 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(Exceptione){ e.printStackTrace(); }finally{ try{ connection.close(); }catch(Exceptione){ e.printStackTrace(); } } } }
其中还需要在web.xml中进行配置
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>CheckServlet</servlet-name> <servlet-class>CheckServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckServlet</servlet-name> <url-pattern>/CheckServlet</url-pattern> </servlet-mapping> </web-app>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。