<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 -----本博客已經(jīng)搬家了,新的地址是 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;
       //創(chuàng)建一個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){}
              }
             }
            }
       //發(fā)送請求函數(shù)
       function send(url){
        createXMLHttpRequest();
        XMLHttpReq.open("GET",url,true);
        XMLHttpReq.onreadystatechange=proce;   //指定響應的函數(shù)
        XMLHttpReq.send(null);  //發(fā)送請求
        }
       function proce(){
        if(XMLHttpReq.readyState==4){ //對象狀態(tài)
         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.創(chuàng)建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");
      //創(chuàng)建輸出流
      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 找個美女做老婆 閱讀(2210) 評論(1)  編輯  收藏

    評論

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

    額外認為v  回復  更多評論   


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


    網(wǎng)站導航:
     

    導航

    統(tǒng)計

    公告

    本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品麻豆免费版| 在线观看免费为成年视频| 国产国拍亚洲精品福利 | 国产精品永久免费视频| 国产无遮挡色视频免费视频| 亚洲a级在线观看| 无码日韩精品一区二区免费| 亚洲精品第一国产综合野| 免费大片黄在线观看yw| 中文字幕 亚洲 有码 在线| 永久免费AV无码国产网站 | 亚洲精品免费在线| ww4545四虎永久免费地址| 亚洲免费二区三区| 女人让男人免费桶爽30分钟| 亚洲AV无码一区二区一二区| 免费人妻无码不卡中文字幕18禁| 人妻仑刮八A级毛片免费看| 亚洲一区无码精品色| 日本中文字幕免费高清视频| 亚洲精品人成电影网| 免费黄色app网站| 亚洲阿v天堂在线2017免费| 亚洲AV无码专区国产乱码4SE| 91精品成人免费国产片| 亚洲国产成人精品激情| 国产一区二区三区无码免费| 一级成人a免费视频| 久久久久亚洲精品天堂| 成人性生活免费视频| 成人免费观看男女羞羞视频| 亚洲AV成人精品网站在线播放| 在线视频免费观看高清| 国产亚洲精品成人久久网站| 亚洲精品国精品久久99热一| 在线视频免费观看爽爽爽| 污网站在线观看免费| 久久久久亚洲AV片无码下载蜜桃| 日韩免费观看视频| 国产成人免费ā片在线观看老同学| 亚洲综合色丁香麻豆|