- <scripttype="text/javascript"language="javascript">
- varxmlHttp=false;
- try{
- xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
- }catch(e){
- try{
- xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
- }catch(e2){
- xmlHttp=false;
- }
- }
- if(!xmlHttp&&typeofXMLHttpRequest!='undefined'){
- xmlHttp=newXMLHttpRequest();
- }
- //检测用户名是否存在
- functioncallServerCheckUname(){
- //通过GET方法请求,将请求封装在URL中
- varuname=document.getElementById("u_name").value;
- xmlHttp.open("POST",'CheckUname?u_name='+uname);
- //设置服务器响应的处理方法
- xmlHttp.onreadystatechange=updatePageCheckUname;
- //发送请求
- xmlHttp.send(null);
- }
- functionupdatePageCheckUname(){
- if(xmlHttp.readyState==4){
- varresponse=xmlHttp.responseText;
- if(response>=1){
- alert("用户名已存在!");
- }else{
- alert("可注册!");
- }
- }
- }
- </script>
<script type="text/javascript" language="javascript"> var xmlHttp = false; try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } } if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); } //检测用户名是否存在 function callServerCheckUname() { //通过GET方法请求,将请求封装在URL中 var uname = document.getElementById("u_name").value; xmlHttp.open("POST",'CheckUname?u_name='+uname); //设置服务器响应的处理方法 xmlHttp.onreadystatechange = updatePageCheckUname; // 发送请求 xmlHttp.send(null); } function updatePageCheckUname() { if (xmlHttp.readyState == 4) { var response = xmlHttp.responseText; if(response >= 1){ alert("用户名已存在!"); }else{ alert("可注册!"); } } } </script>
web.xml配置文件:
301@
<servlet>
<servlet-name>CheckUname</servlet-name>
<servlet-class>servlet.AjaxCheckUname</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckUname</servlet-name>
<url-pattern>/CheckUname</url-pattern>
</servlet-mapping>
@H_140_<servlet> <servlet-name>CheckUname</servlet-name> <servlet-class>servlet.AjaxCheckUname</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckUname</servlet-name> <url-pattern>/CheckUname</url-pattern> </servlet-mapping>
servlet页面:
- publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException{
- response.setContentType("text/html;charset=GB2312");
- PrintWriterout=response.getWriter();
- try{
- Stringuname=request.getParameter("u_name");
- RegistDAOImplregDao=newRegistDAOImpl();
- out.print(regDao.checkUname(uname));
- }catch(Exceptione){
- }
- out.flush();
- out.close();
- }
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); try { String uname = request.getParameter("u_name"); RegistDAOImpl regDao = new RegistDAOImpl(); out.print(regDao.checkUname(uname)) ; } catch (Exception e) { } out.flush(); out.close(); }
DAO实现类:
- publicintcheckUname(Stringuname){
- Connectionconn=null;
- PreparedStatementpstm=null;
- ResultSetrs=null;
- intsuccess=0;
- Stringsql="selectu_namefromu_infowhereu_name='"+uname+"'oru_mobtel='"+uname+"'oru_email='"+uname+"'";
- try{
- conn=getConnection();
- pstm=conn.prepareStatement(sql);
- rs=pstm.executeQuery(sql);
- if(rs.next()){
- success=1;
- }
- returnsuccess;
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- try{
- conn.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- returnsuccess;
- }