Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          一個簡單的利用Ajax和Servlet進行登錄用戶名檢查例子

          1.這個是頁面,負責登錄操作

          <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
          <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
          <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
          <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
          <%@ page contentType="text/html;charset=GBK" pageEncoding="GB2312"%>
          <html:html>
          <head>
          <title>
          AddUser
          </title>
          <LINK
          href="images/css.css" type=text/css rel=stylesheet>
            <script>
            
            //設一個變量
            
            var XMLHttpReq=false;
             //創建一個XMLHttpRequest對象
             function createXMLHttpRequest(){
               if(window.XMLHttpRequest){ //Mozilla
                XMLHttpReq=new XMLHttpRequest();
                }
                else if(window.ActiveXObject){
                 try{
                  XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
                  }catch(e){
                   try{
                    XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
                    }catch(e){}
                    }
                   }
                  }
             //發送請求函數
             function send(url){
              createXMLHttpRequest();
              XMLHttpReq.open("GET",url,true);
              XMLHttpReq.onreadystatechange=proce;   //指定響應的函數
              XMLHttpReq.send(null);  //發送請求
              }
             function proce(){
              if(XMLHttpReq.readyState==4){ //對象狀態
               if(XMLHttpReq.status==200){//信息已成功返回,開始處理信息
               var res=XMLHttpReq.responseXML.getElementsByTagName("content")[0].firstChild.data;
               window.alert(res);
               }else{
                window.alert("所請求的頁面有異常");
                }
                }
                }
             //身份驗證
             function check(){
              var name=document.getElementById("salesname").value;
               
               if(name==""){
                alert("請輸入姓名");
                return false;
                }
                else{
                 send('checkuser?name='+name);
                 }
                }
                
             </script>
          </head>
          <body>
          <p>
          <table width="801" border="0" align="center">
            <tr>
              <th width="408" height="29" background="images/bg1.gif" scope="col">&nbsp;</th>
            </tr>
          </table>
          <table width="801" border="0" align="center">
            <tr>
              <th width="795" align="center" background="images/bg1_2.gif" scope="col">&nbsp;</th>
            </tr>
          </table>
          <h1 align="center">&nbsp;</h1>
          <h4 style="color:red" align="center"><html:errors/></h4>
          <html:form action="/adduserAction.do" method="POST">
          <table align="center">
           <tr>
            <td>賬號</td>
            <td><html:text property="salesname"/></td>
             <td align="left" style="color:red">*</td>
             <td><input type="button" value="檢查用戶名" onClick="check()"/></td>
           </tr>
            <tr>
            <td>密碼</td>
            <td><html:password property="password"/></td>
             <td align="left" style="color:red">*</td>
           </tr>
            <tr>
            <td>密碼確認</td>
            <td><html:password property="repassword"/></td>
             <td align="left" style="color:red">*</td>
           </tr>
            <tr>
            <td>真實姓名</td>
            <td><html:text property="realname"/></td>
             <td align="left" style="color:red">*</td>
           </tr>
            <tr>
            <td>性別</td>
            <td><html:radio property="sex" value="男">男
          </html:radio>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<html:radio property="sex" value="女">女
          </html:radio></td>
           <td align="left" style="color:red">*</td>
           </tr>
            <tr>
            <td></td>
            <td><input type="image" src="images/ok.jpg" name="pic"/></td>
            <td><a href="login.jsp"><img src="images/backbutton.gif" width="70" height="20"></a></td>
            <td></td>
           </tr>
          </table>

          <br>

          <br>

          <br>

          <br>
          <br />
          </html:form>
          </body>
          </html:html>

          2.創建Servlet

          package com.ajaxservlet;

          import java.io.IOException;
          import java.io.PrintWriter;

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

          import com.jdbc.TDB;

          public class CheckUser extends HttpServlet {

           /**
            * 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 {
            //設置接收的信息的字符集
            //設置接收的信息的字符集
            request.setCharacterEncoding("UTF-8");
            
            String name=request.getParameter("name");
            
            //設置輸出的信息的格式及字符集
            response.setContentType("text/xml; charset=UTF-8");
            response.setHeader("Cache-Control","no-cache");
            //創建輸出流
            PrintWriter out=response.getWriter();
            //查找用戶名相關操作
            boolean flag=new TDB().ajaxcheckuser(name);
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println("  <BODY>");
            if(flag==true){
             out.println("<content>"+"賬戶已存在,請重新注冊"+"</content>");
            
            }else{
             out.println("<content>"+"賬戶不存在,可以注冊"+"</content>");
            }
            out.println("  </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
           }

           /**
            * 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 {

            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<HTML>");
            out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println("  <BODY>");
            out.print("    This is ");
            out.print(this.getClass());
            out.println(", using the POST method");
            out.println("  </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
           }

           /**
            * Initialization of the servlet. <br>
            *
            * @throws ServletException if an error occure
            */
           public void init() throws ServletException {
            // Put your code here
           }

          }

          3.Web.xml文件配置

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            <welcome-file-list>
               <welcome-file>/login.jsp</welcome-file>
            </welcome-file-list>
            <servlet>
              <servlet-name>addaction</servlet-name>
              <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
              <init-param>
                <param-name>config</param-name>
                <param-value>/WEB-INF/struts-config.xml</param-value>
              </init-param>
              <init-param>
                <param-name>debug</param-name>
                <param-value>3</param-value>
              </init-param>
              <init-param>
                <param-name>detail</param-name>
                <param-value>3</param-value>
              </init-param>
              <load-on-startup>0</load-on-startup>
            </servlet>
            <servlet>
              <description>This is the description of my J2EE component</description>
              <display-name>This is the display name of my J2EE component</display-name>
              <servlet-name>CheckUser</servlet-name>
              <servlet-class>com.ajaxservlet.CheckUser</servlet-class>
            </servlet>

            <servlet-mapping>
              <servlet-name>addaction</servlet-name>
              <url-pattern>*.do</url-pattern>
            </servlet-mapping>
            <servlet-mapping>
              <servlet-name>CheckUser</servlet-name>
              <url-pattern>/checkuser</url-pattern>
            </servlet-mapping>
          </web-app>

          posted on 2009-10-12 17:36 找個美女做老婆 閱讀(2207) 評論(1)  編輯  收藏

          評論

          # re: 一個簡單的利用Ajax和Servlet進行登錄用戶名檢查例子[未登錄] 2013-12-11 14:05

          額外認為v  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          本blog已經搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青岛市| 武定县| 大余县| 彰化县| 印江| 阳泉市| 山东省| 徐州市| 屏东县| 祁门县| 渝中区| 股票| 衡山县| 鄂尔多斯市| 阿合奇县| 长顺县| 霍州市| 福建省| 曲松县| 阿坝县| 澄江县| 古浪县| 澜沧| 开阳县| 巴南区| 锦州市| 日喀则市| 罗山县| 中牟县| 光泽县| 鞍山市| 贡嘎县| 松阳县| 兴安盟| 英吉沙县| 蓬莱市| 库伦旗| 仙桃市| 乌拉特中旗| 甘孜| 陈巴尔虎旗|