Ajax入門

來源:互聯網
上載者:User

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <TITLE>Ajax驗證使用者名稱是否存在例子</TITLE>
  <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){//資訊已成功返回,開始處理資訊   
                 <!--測試讀取xml開始-->
     var root=XMLHttpReq.responseXML;
     var res=root.getElementsByTagName("content")[0].firstChild.data;
     window.alert(res);
      <!--測試讀取xml結束-->  
     //var xmlReturn = XMLHttpReq.responseText;
     //window.alert(xmlReturn);
     }else{
      window.alert("所請求的頁面有異常");
      }
      }
      }
   //身分識別驗證
   function check(){
    var name=document.getElementById("name").value;
    
     if(name==""){
      alert("請輸入姓名!");
      return false;
      }
      else{
       //send('login?name='+name); 
       document.getElementById("load").style.display='';   
       send('login.do?name='+name);
       document.getElementById("load").style.display='none';         
       }
      }
     
   </script>
  </head>
 
  <body>
  <form action="login" method="post">
   <table>
       <tr><td>姓名:&nbsp;<input id="name" type="text" name="name"/><p>
        <div id=load style="display:none; position:absolute;right:0px;top:0px;background:#FF5B5B;border:1px solid">正在驗證使用者名稱,請稍後……</div><p>
          <input type="button" value="檢測!" onClick="check()"/>
       </td>
      </tr>
      </table>
      </form>
  </body>
</html>

 

structs-config配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="loginForm" type="org.viman.struts.form.LoginForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="loginForm"
      input="/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="org.viman.struts.action.LoginAction" />

  </action-mappings>

  <message-resources parameter="org.viman.struts.ApplicationResources" />
</struts-config>

 

action實現(LoginAction.java):

package org.viman.struts.action;

import java.io.UnsupportedEncodingException;

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


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.viman.struts.form.LoginForm;

/**
 * MyEclipse Struts
 * Creation date: 01-06-2007
 *
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
 */
public class LoginAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  //LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  try {
      request.setCharacterEncoding("gb2312");
     } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
     }
     try{
      String msgStr="";
      response.setContentType("text/xml;charset=GB2312");
      response.setHeader("Cache-Control","no-cache");
      String name=(String)request.getParameter("name");
      System.out.println("name = "+name);
      if("gong".equals(name)){ //找查資料庫中有無該使用者名稱
      msgStr ="對不起,此使用者名稱已經存在,請更換使用者名稱註冊!";
      }else{
      msgStr ="使用者未被註冊,可以使用!";
      }
      response.getWriter().println("<?xml version='1.0' encoding='GB2312' ?>");
      response.getWriter().println("<root>");
      response.getWriter().println("<content>");
      response.getWriter().print(msgStr);
      response.getWriter().println("</content>");
      response.getWriter().println("</root>");
      response.getWriter().close();
      }catch(Exception ex){

      } 
     return null;


 }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.