JAVA涂鴉
          關(guān)于JAVA的點點滴滴
          posts - 50,  comments - 689,  trackbacks - 0
          ajax技術(shù)現(xiàn)在越來越受到大家的歡迎,因為它能很好的解決一些以前不好解決的問題,動態(tài)檢查文本框中的數(shù)據(jù)就是體現(xiàn)之一。現(xiàn)在已經(jīng)加上了數(shù)據(jù)庫mysql,可以說得上是一個比較完整的例子了。

          下面就是怎樣實現(xiàn)動態(tài)檢查文本框中的數(shù)據(jù)一個例子:
          工程目錄如下:
          1.jpg

          CheckServlet.java:
          /**
          ?*?
          ?
          */
          package?com;

          import?java.io.IOException;

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

          /**
          ?*?
          @author?Administrator
          ?*
          ?
          */
          public?class?CheckServlet?extends?HttpServlet?{

          ????
          public?String[]?usernameList;
          ????
          ????
          public?CheckServlet()?{
          ????????
          super();
          ????????
          //?TODO?Auto-generated?constructor?stub
          ????}
          ????
          ????
          private?boolean?IsContain(String?param){
          ????????
          for(int?i=0;i<usernameList.length;i++){
          ????????????
          if(usernameList[i].equals(param)){
          ????????????????
          return?true;
          ????????????}
          else{
          ????????????????
          continue;
          ????????????}
          ????????}
          ????????
          return?false;
          ????}
          ????
          ????
          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(username.equals("")||username==null){
          ????????????request.setAttribute(
          "error.message","用戶名不能為空!");
          ????????????RequestDispatcher?requsetDispatcher
          =request.getRequestDispatcher("error.jsp");
          ????????????requsetDispatcher.forward(request,response);
          ????????}
          else?if(password.equals("")||password==null){
          ????????????request.setAttribute(
          "error.message","密碼不能為空!");
          ????????????RequestDispatcher?requsetDispatcher
          =request.getRequestDispatcher("error.jsp");
          ????????????requsetDispatcher.forward(request,response);
          ????????}
          else?if(!password.equals(repassword)){
          ????????????request.setAttribute(
          "error.message","兩次輸入的密碼不一致!");
          ????????????RequestDispatcher?requsetDispatcher
          =request.getRequestDispatcher("error.jsp");
          ????????????requsetDispatcher.forward(request,response);
          ????????}
          else?if(this.IsContain(username)){
          ????????????request.setAttribute(
          "error.message","您輸入的用戶名已經(jīng)存在!");
          ????????????RequestDispatcher?requsetDispatcher
          =request.getRequestDispatcher("error.jsp");
          ????????????requsetDispatcher.forward(request,response);
          ????????}
          else{
          ????????????RequestDispatcher?requsetDispatcher
          =request.getRequestDispatcher("success.jsp");
          ????????????requsetDispatcher.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"};
          ????}
          ????
          }

          PreServlet.java:
          package?com;

          import?java.io.IOException;
          import?java.io.PrintWriter;
          import?java.util.Iterator;
          import?java.util.List;

          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?List?usernameList;?
          ????
          /**
          ?????*?Constructor?of?the?object.
          ?????
          */
          ????
          public?PreServlet()?{
          ????????
          super();
          ????}

          ????
          /**
          ?????*?Destruction?of?the?servlet.?<br>
          ?????
          */
          ????
          public?void?destroy()?{
          ????????
          super.destroy();?//?Just?puts?"destroy"?string?in?log
          ????????
          //?Put?your?code?here
          ????}

          ????
          /**
          ?????*?The?doGet?method?of?the?servlet.?<br>
          ?????*
          ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?get.
          ?????*?
          ?????*?
          @param?request?the?request?send?by?the?client?to?the?server
          ?????*?
          @param?response?the?response?send?by?the?server?to?the?client
          ?????*?
          @throws?ServletException?if?an?error?occurred
          ?????*?
          @throws?IOException?if?an?error?occurred
          ?????
          */
          ????
          public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
          ????????????
          throws?ServletException,?IOException?{
          ????????response.setContentType(
          "text/xml");
          ????????response.setHeader(
          "Cache-Control","no-cache");
          ????????String?username
          =(String)request.getParameter("user_name");
          ????????System.out.println(username);
          ????????String?xml
          ="<?xml?version=\"1.0\"?encoding=\"UTF-8\"?>";
          ????????
          if(username.equals("")||username==null){
          ????????????xml
          +="<message><info>Username?is?required!</info></message>";
          ????????}
          ????????
          else?if(this.IsContain(username)){
          ????????????xml
          +="<message><info>The?username?has?existes,Please?choose?other?username!</info></message>";
          ????????}
          ????
          else{
          ????????????xml
          +="<message><info>Username?is?approved!</info></message>";
          ????????}
          ????????response.getWriter().write(xml);
          ????}

          ????
          /**
          ?????*?The?doPost?method?of?the?servlet.?<br>
          ?????*
          ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?post.
          ?????*?
          ?????*?
          @param?request?the?request?send?by?the?client?to?the?server
          ?????*?
          @param?response?the?response?send?by?the?server?to?the?client
          ?????*?
          @throws?ServletException?if?an?error?occurred
          ?????*?
          @throws?IOException?if?an?error?occurred
          ?????
          */
          ????
          public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
          ????????????
          throws?ServletException,?IOException?{

          ????????doGet(request,response);
          ????}

          ????
          /**
          ?????*?Initialization?of?the?servlet.?<br>
          ?????*
          ?????*?
          @throws?ServletException?if?an?error?occure
          ?????
          */
          ????
          public?void?init()?throws?ServletException?{
          ????????
          //?Put?your?code?here
          ????????DBOperator?dBOperator=new?DBOperator();
          ????????usernameList
          =dBOperator.getUsernameList();
          ????}
          ????
          ????
          private?boolean?IsContain(String?param){
          ??? ?? //這里修改了以前的代碼,現(xiàn)在是讀取數(shù)據(jù)庫的數(shù)據(jù)。
          ????????Iterator?it
          =usernameList.iterator();
          ????????
          while(it.hasNext()){
          ????????????
          if(it.next().toString().equals(param)){
          ????????????????
          return?true;
          ????????????}
          else{
          ????????????????
          continue;
          ????????????}
          ????????}
          ????????
          return?false
          ;
          ????}
          }


          DBOperator.java:
          package?com;

          import?java.sql.Connection;
          import?java.sql.ResultSet;
          import?java.sql.Statement;
          import?java.util.ArrayList;
          import?java.util.List;

          public?class?DBOperator?{
          ????
          public?DBOperator(){}
          ????
          ????
          public?Connection?Connect(){
          ????????Connection?con?
          =?null;
          ????????
          try?{
          ????????????Class.forName(
          "com.mysql.jdbc.Driver").newInstance();
          ????????????con?
          =?java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","");
          ????????}?
          catch?(Exception?e)?{
          ????????????
          //?TODO?Auto-generated?catch?block
          ????????????e.printStackTrace();
          ????????}
          ????????
          return?con;
          ????}
          ????
          ????
          public?List?getUsernameList(){
          ????????Connection?con
          =Connect();
          ????????List?usernameList
          =new?ArrayList();
          ????????
          try?{
          ????????????Statement?stmt
          =con.createStatement();
          ????????????ResultSet?rst
          =stmt.executeQuery("select?*?from?m_stuinfo");
          ????????????
          while(rst.next()){
          ????????????????usernameList.add(rst.getString(
          "m_stuinfo_name"));
          ????????????}
          ????????}?
          catch?(Exception?e)?{
          ????????????
          //?TODO?Auto-generated?catch?block
          ????????????e.printStackTrace();
          ????????}
          ????????
          return?usernameList;
          ????}
          }


          index.jsp:
          <%@?page?language="java"?contentType="text/html;?charset=utf-8"%>
          <html>
          ????
          <head>
          ????????????
          <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
          ????????????
          <title>注冊</title>
          ????????????
          <SCRIPT?type="text/javascript">
          ????????????????var?req;
          ????????????????function?UsrNameCheck()
          ????????????????{
          ????????????????????var?username
          =document.getElementById('username').value;
          ????????????????????var?url
          ="pre?user_name="+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();
          ????????????????????????????}
          else{
          ????????????????????????????????alert(
          "Not?able?to?retrieve?description"+req.statusText);
          ????????????????????????????}
          ????????????????????????}
          else{
          ????????????????????????????document.getElementById(
          'check_username').innerHTML="正在驗證用戶名";
          ????????????????????????}
          ????????????????????}
          ????????????????????
          ????????????????????function?parseMessage()
          ????????????????????{
          ????????????????????????var?xmlDoc
          =req.responseXML.documentElement;
          ????????????????????????alert(xmlDoc);
          ????????????????????????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="/reg/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="UsrNameCheck()">
          ????????????????????????
          <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">重復(fù)密碼:?</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>

          success.jsp:
          <%@?page?language="java"?contentType="text/html;?charset=GB2312"%>
          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
          <html>
          <head>
          <meta?http-equiv="Content-Type"?content="text/html;?charset=GB2312">
          <title>注冊成功</title>
          </head>
          <body>
          ????恭喜你,注冊成功!
          <br>
          </body>
          </html>

          error.jsp:
          <%@?page?language="java"?contentType="text/html;?charset=GB2312"%>
          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
          <html>
          ????
          <head>
          ????????
          <meta?http-equiv="Content-Type"?content="text/html;?charset=GB2312">
          ????????
          <title>失敗</title>
          ????
          </head>
          ????
          <body>
          ????????
          <div?align="center">
          ????????????
          <table?width="70%"?border="1">
          ????????????????
          <tr>
          ????????????????????
          <td>注冊失敗</td>
          ????????????????
          </tr>
          ????????????????
          <tr>
          ????????????????????
          <td>&nbsp;<%=(String)request.getAttribute("error.message")?%></td>
          ????????????????
          </tr>
          ????????????????
          <tr>
          ????????????????????
          <td><a?href="index.jsp">返回注冊頁</a></td>
          ????????????????
          </tr>
          ????????????
          </table>
          ????????
          </div>
          ????
          </body>
          </html>

          web.xml:
          <?xml?version="1.0"?encoding="UTF-8"?>
          <web-app?version="2.4"?
          ????xmlns
          ="http://java.sun.com/xml/ns/j2ee"?
          ????xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"?
          ????xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee?
          ????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
          ????
          ????
          <display-name>reg</display-name>
          ????
          ????
          <!--?Action?Servlet?Configuration?-->
          ????
          <servlet>
          ????????
          <servlet-name>regservlet</servlet-name>
          ????????
          <servlet-class>com.CheckServlet</servlet-class>
          ????
          </servlet>
          ????
          <servlet>
          ????????
          <servlet-name>PreServlet</servlet-name>
          ????????
          <servlet-class>com.PreServlet</servlet-class>
          ????
          </servlet>

          ????
          ????
          <!--?Action?Servlet?Mapping?-->
          ??????
          <servlet-mapping>
          ????????
          <servlet-name>regservlet</servlet-name>
          ????????
          <url-pattern>/regservlet</url-pattern>
          ??????
          </servlet-mapping>
          ????
          <servlet-mapping>
          ????????
          <servlet-name>PreServlet</servlet-name>
          ????????
          <url-pattern>/pre</url-pattern>
          ????
          </servlet-mapping>
          ??????
          ????
          <welcome-file-list>
          ????????
          <welcome-file>index.jsp</welcome-file>
          ????
          </welcome-file-list>
          </web-app>

          運行結(jié)果是:
          2.JPG
          posted on 2006-05-16 13:11 千山鳥飛絕 閱讀(3680) 評論(6)  編輯  收藏 所屬分類: Ajax

          FeedBack:
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-16 18:25 | 黑蝙蝠
          這書我也買了 有幾個問題 :
          1.你要是輸入中文用戶名 那就無法驗證
          2.usernameList=new String[]{"Tom","Jerry","黑蝙蝠"};
          你把這里改成中文也無法認(rèn)證
          3.我想從數(shù)據(jù)庫里取數(shù)據(jù),結(jié)果不知道是什么原因,也無法正常完成。  回復(fù)  更多評論
            
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-17 10:20 | 千山鳥飛絕
          1、我修改了一下代碼,現(xiàn)在可以使用中文了。只需將var url="pre?user_name="+escape(username);中的escape()去掉就可以了 。escape()是為 ASCII字符做轉(zhuǎn)換工作的,這里并不需要。
          2、連接數(shù)據(jù)庫操作,我有空時會做一做的。  回復(fù)  更多評論
            
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-17 18:32 | 聶衛(wèi)國
          太復(fù)雜了
            回復(fù)  更多評論
            
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-18 13:07 | 千山鳥飛絕
          今天加上了數(shù)據(jù)庫--mysql,更改了一些代碼,增加了一個DBOperator類。  回復(fù)  更多評論
            
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-18 14:23 | 黑蝙蝠
          原來escape()搞的鬼
          但是escape()到底什么時候該用 什么時候不該用?
          運行是否成功呢? 從數(shù)據(jù)庫里取出的數(shù)據(jù)也應(yīng)該是正常的吧。  回復(fù)  更多評論
            
          # re: 動態(tài)檢查文本框中的數(shù)據(jù)
          2006-05-18 16:11 | 千山鳥飛絕
          我覺得escape()沒必要用,雖然我對Js了解也不是很深刻。  回復(fù)  更多評論
            
          正在閱讀:



          <2006年5月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(35)

          隨筆檔案

          文章分類

          文章檔案

          好友的blog

          我的其他blog

          老婆的Blog

          搜索

          •  

          積分與排名

          • 積分 - 775366
          • 排名 - 56

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 津南区| 大名县| 贡嘎县| 田东县| 华蓥市| 平定县| 女性| 深州市| 东平县| 启东市| 石渠县| 汽车| 山东省| 上饶市| 东宁县| 石景山区| 广东省| 昭觉县| 陆河县| 桐城市| 色达县| 济源市| 乐亭县| 邳州市| 台江县| 镇江市| 黄龙县| 杭锦旗| 潼南县| 河北区| 平邑县| 柘荣县| 阳朔县| 吕梁市| 池州市| 左贡县| 平度市| 丘北县| 房产| 双柏县| 饶河县|