Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1966483
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类:

2008-09-30 22:03:10

运行效果图:

 
index.jsp
 

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
    <head>
        <title>
            注册
        </title>
    <script type="text/javascript">
    var req;
    
    function UserNameCheck()
    {
    var username = document.getElementById('username').value;

         var url = "/test/pre?user_name=" + escape(username);

         if (window.XMLHttpRequest) {

             req = new XMLHttpRequest();

         }else if (window.ActiveXObject) {

             req = new ActiveXObject("Microsoft.XMLHTTP");

         }

        if(req){

            req.open("GET", url, true);

             req.onreadystatechange = callback;

            req.send(null);

         }    
    
    }    
    
    function callback() {
    
    
    if (req.readyState == 4) {
    
        if (req.status == 200) {

                 parseMessage();

                 // update the HTML DOM based on whether or not message is valid

        }else{

            parseMessage();
            
          // alert ("Not able to retrieve description" + req.statusText);

        }

    }
    else
    {
        document.getElementById("check_username").innerHTML = "正在验证用户名....";
    }
   }
   
   
   function parseMessage() {
   
    var xmlDoc = req.responseXML.documentElement;
    
    var node = xmlDoc.getElementsByTagName('info');
    
    document.getElementById('check_username').innerHTML = node[0].firstChild.nodeValue;
    
    
    }
    
    
    function Form_Submit()
    {
        if(regForm.username.value=="")
        {
         alert("用户名不能为空!");
         return false;
        }
        else if(regForm.password.value=="")
        {
         alert("密码不能为空!");
         return false;
        }
        else if(regForm.password.value!=regForm.repassword.value)
        {
        alert("两次输入的密码不一致!");
         return false;
        }
        regForm.submit();
    }
    
    </script>

    </head>


    <body>
        <div align="center">
            <form name="regForm" method="post" action="/test/regservlet">
                <table width="70%" border="1">
                    <tr align="center">
                        <td colspan="2">
                            用户注册
                        </td>
                    </tr>
                    <tr>
                        <td width="24%" align="center">
                            用户名:
                        </td>
                        <td width="76%">
                            <input name="username" type="text" id="username" onBlur="UserNameCheck()">
                            <span id="check_username"></span>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            密码:
                        </td>
                        <td>
                            <input name="password" type="password" id="password">
                            
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            重复密码:
                        </td>
                        <td>
                            <input name="repassword" type="password" id="repassword">
                            
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            email:
                        </td>
                        <td>
                            <input name="email" type="text" id="email">
                        </td>
                    </tr>
                    <tr align="center">
                        <td colspan="2">
                            <input type="button" name="Submit" value="按钮" onClick="Form_Submit()">
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="
    /web-app_2_4.xsd"
>

    <servlet>
        <servlet-name>preServlet</servlet-name>
        <servlet-class>AjaxLucene.CH01.cn.edu.bit.servlet.PreServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>preServlet</servlet-name>
        <url-pattern>/pre</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>regServlet</servlet-name>
        <servlet-class>AjaxLucene.CH01.cn.edu.bit.servlet.CheckServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>regServlet</servlet-name>
        <url-pattern>/regservlet</url-pattern>
    </servlet-mapping>
</web-app>

 

PreServlet.java

package AjaxLucene.CH01.cn.edu.bit.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PreServlet extends HttpServlet {

    public String[] usernameList;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        response.setContentType("text/xml;charset=UTF-8");

     response.setHeader("Cache-Control", "no-cache");    

        String username = (String) request.getParameter("user_name");
        
        String xml = "";

        if (username.equals("") || username == null) {
            
            xml +="Username is required !";
        }else if(this.IsContain(username))
        {
            xml += "The username has exsites,Please choose other username!";
        }
        else
        {
            xml += "合法!";
        }
        
        response.getWriter().write(xml);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doGet(request, response);
    }

    public void init(ServletConfig config) throws ServletException {
        usernameList = new String[] { "Tom", "Jerry", "Brain" };
    }

    public boolean IsContain(String param) {
        for (int i = 0; i < usernameList.length; i++) {
            if (usernameList[i].equals(param)) {
                return true;
            } else
                continue;
        }

        return false;
    }

}

 

CheckServlet.java

package AjaxLucene.CH01.cn.edu.bit.servlet;
 
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.RequestDispatcher;
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 String[] usernameList;
    
    public void doGet(HttpServletRequest request,HttpServletResponse response)
     throws IOException,ServletException
    {
        String username = (String)request.getParameter("username");
        
        String password = (String)request.getParameter("password");
        
        String repassword = (String)request.getParameter("repassword");
        
        String email = (String)request.getParameter("email");
        
        if(this.IsContain(username))
        {
            request.setAttribute("error.message","您输入的用户名已经存在!");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("AjaxLucene/CH01/error.jsp");
            requestDispatcher.forward(request,response);    
        }else
        {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("AjaxLucene/CH01/success.jsp");
            requestDispatcher.forward(request,response);
        }
    
    }
    
    public void doPost(HttpServletRequest request,HttpServletResponse response)
     throws IOException,ServletException
     {
        doGet(request,response);
     }
    
     public void init(ServletConfig config) throws ServletException {
         usernameList = new String[]{"Tom","Jerry","Brain"};
     }
    
     public boolean IsContain(String param)
     {
         for(int i = 0; i <usernameList.length;i++)
         {
             if(usernameList[i].equals(param))
             {
                 return true;
             }
             else
                 continue;
         }
        
         return false;
     }
}

阅读(774) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~