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

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

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

    Java學(xué)習(xí)

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

     

    一個(gè)簡(jiǎn)單的利用Ajax和Servlet進(jìn)行登錄用戶名檢查例子

    1.這個(gè)是頁面,負(fù)責(zé)登錄操作

    <%@ 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>
      
      //設(shè)一個(gè)變量
      
      var XMLHttpReq=false;
       //創(chuàng)建一個(gè)XMLHttpRequest對(duì)象
       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ā)送請(qǐng)求函數(shù)
       function send(url){
        createXMLHttpRequest();
        XMLHttpReq.open("GET",url,true);
        XMLHttpReq.onreadystatechange=proce;   //指定響應(yīng)的函數(shù)
        XMLHttpReq.send(null);  //發(fā)送請(qǐng)求
        }
       function proce(){
        if(XMLHttpReq.readyState==4){ //對(duì)象狀態(tài)
         if(XMLHttpReq.status==200){//信息已成功返回,開始處理信息
         var res=XMLHttpReq.responseXML.getElementsByTagName("content")[0].firstChild.data;
         window.alert(res);
         }else{
          window.alert("所請(qǐng)求的頁面有異常");
          }
          }
          }
       //身份驗(yàn)證
       function check(){
        var name=document.getElementById("salesname").value;
         
         if(name==""){
          alert("請(qǐng)輸入姓名");
          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>賬號(hào)</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>密碼確認(rèn)</td>
      <td><html:password property="repassword"/></td>
       <td align="left" style="color:red">*</td>
     </tr>
      <tr>
      <td>真實(shí)姓名</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 {
      //設(shè)置接收的信息的字符集
      //設(shè)置接收的信息的字符集
      request.setCharacterEncoding("UTF-8");
      
      String name=request.getParameter("name");
      
      //設(shè)置輸出的信息的格式及字符集
      response.setContentType("text/xml; charset=UTF-8");
      response.setHeader("Cache-Control","no-cache");
      //創(chuàng)建輸出流
      PrintWriter out=response.getWriter();
      //查找用戶名相關(guān)操作
      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>"+"賬戶已存在,請(qǐng)重新注冊(cè)"+"</content>");
      
      }else{
       out.println("<content>"+"賬戶不存在,可以注冊(cè)"+"</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 找個(gè)美女做老婆 閱讀(2219) 評(píng)論(1)  編輯  收藏

    評(píng)論

    # re: 一個(gè)簡(jiǎn)單的利用Ajax和Servlet進(jìn)行登錄用戶名檢查例子[未登錄] 2013-12-11 14:05

    額外認(rèn)為v  回復(fù)  更多評(píng)論   


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


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計(jì)

    公告

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

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲欧洲日韩国产综合在线二区| 无码国产亚洲日韩国精品视频一区二区三区| 国产亚洲美女精品久久久2020| 色屁屁www影院免费观看视频| 国产精品免费看香蕉| 亚洲av无码无线在线观看| 暖暖免费高清日本中文| 亚洲av永久无码一区二区三区| 青苹果乐园免费高清在线| 四虎亚洲精品高清在线观看| 成人无遮挡裸免费视频在线观看 | 久久精品国产亚洲av麻豆蜜芽| 97视频免费观看2区| 亚洲午夜精品在线| 毛片网站免费在线观看| 亚洲高清毛片一区二区| av无码东京热亚洲男人的天堂| gogo免费在线观看| 亚洲国产成人久久精品影视 | 免费观看黄网站在线播放| 亚洲码和欧洲码一码二码三码| 免费国产在线观看老王影院| 九一在线完整视频免费观看| 亚洲AV无码一区二区乱子伦| 亚洲国产天堂久久久久久| 免费的黄色的网站| 亚洲AV无码成人网站在线观看 | 国产亚洲美女精品久久久| 日本一区二区免费看| 亚洲另类视频在线观看| 成年女人永久免费观看片| 亚洲黄片手机免费观看| 亚洲邪恶天堂影院在线观看| 好大好硬好爽免费视频| av成人免费电影| 亚洲性色高清完整版在线观看| 永久在线毛片免费观看| 91国内免费在线视频| 亚洲AV色吊丝无码| 亚洲中文字幕无码久久综合网| 2021国内精品久久久久精免费 |