<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 找個美女做老婆 閱讀(2209) 評論(1)  編輯  收藏

    評論

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

    額外認為v  回復  更多評論   


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


    網站導航:
     

    導航

    統計

    公告

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

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久久久久夜精品精品免费啦| 国产AV无码专区亚洲AV琪琪| 成人无码a级毛片免费| 国产成人99久久亚洲综合精品| 三年片在线观看免费观看大全中国| 国产免费69成人精品视频| 亚洲国产精品无码观看久久| 永久黄网站色视频免费| 日韩色视频一区二区三区亚洲| 免费国产人做人视频在线观看| 亚洲AV无码专区在线电影成人 | 亚洲免费一级视频| 亚洲男人的天堂在线| 免费99精品国产自在现线| 亚洲久悠悠色悠在线播放| 破了亲妺妺的处免费视频国产| 精品国产日韩亚洲一区在线| 亚洲国产一区视频| 久久免费线看线看| 亚洲香蕉久久一区二区| 国产精品久久免费视频| 九九免费精品视频在这里| 精品国产亚洲一区二区三区| 91久久成人免费| 色九月亚洲综合网| 亚洲午夜福利717| 91精品国产免费久久久久久青草| 亚洲欧美日韩一区二区三区| 中文字幕第13亚洲另类| 免费A级毛片无码A∨中文字幕下载| 久久久久亚洲国产| 丁香五月亚洲综合深深爱| 无码区日韩特区永久免费系列 | 亚洲国产高清国产拍精品| 亚洲精品国产高清嫩草影院| 亚洲a一级免费视频| 久久无码av亚洲精品色午夜| 亚洲精品无码久久久影院相关影片| 中文字幕无码不卡免费视频| 亚美影视免费在线观看| 亚洲jizzjizz在线播放久|