熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

ajax驗證用戶名是否可用

2013-11-23 18:57:19  來源: Java核心技術 

  Ajax很簡單(jsp例子)

  首先是jsp頁面和腳本為了方便寫在一個裡面

  這是一個很常見的檢測用戶名是否存在的功能

  這裡用的是struts 可以在路徑path中加加上要傳的值name 在action中連接數據庫 驗證是否存在

  

  <%@ page contentType=text/html; charset=GBK %>

  <html>

  <head>

  <title>

  ajax

  </title>

  </head>

  <body bgcolor=#ffffff>

  <h>

  <input name=username type=text maxlength= />

  <input id=chknamebtn type=button value=檢測帳號 onclick=testName(<%=requestgetContextPath()%>) />

  <div id=view_name></div>

  </h>

  </body>

  </html>

  <script language=javascript>

  if (windowActiveXObject && !windowXMLHttpRequest) {

  windowXMLHttpRequest=function() {

  return new ActiveXObject((navigatoruserAgenttoLowerCase()indexOf(msie ) != ) ? MicrosoftXMLHTTP : MsxmlXMLHTTP);

  };

  }//取得XMLHttpRequest對象

  function testName(path){

  //path是取得系統路徑

  var view_name=documentgetElementById(view_name);

  var req=new XMLHttpRequest();

  if (req) {

  reqonreadystatechange=function() {

  if (reqreadyState== && reqstatus==) {//判斷狀態是已發送已完成

  if(reqresponseText==){

  lor=green;

  view_nameinnerHTML=該用戶名可以正常使用;

  }else if(reqresponseText==){

  lor=red;

  view_nameinnerHTML=該用戶名已經被使用;

  }else{

  lor=red;

  view_nameinnerHTML=該用戶名含有非法字符!;

  }

  }

  }

  reqopen(POST path+/ajaxdo);//struts

  //reqopen(POST path+/ajaxservlet);//servlet

  //reqopen(POST path+/ajaxaction);//webwork

  reqsetRequestHeader(ContentType application/xwwwformurlencoded);

  reqsend();//發送參數如果有參數reqsend(username=+user_name);用request取得

  }

  }

  </script>

  這個jsp頁面並沒有取得用戶名就是演示一下還有<div>可以換span具體作用問美工吧

  接受ajax請求的action

  import orgapachestrutsaction*;

  import javaxservlethttp*;

  import javaxservlethttpHttpServlet;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import javaioPrintWriter;

  /**

  * <p>Title:AjaxAction </p>

  */

  public class AjaxAction extends Action {

  public ActionForward execute(ActionMapping mapping

  ActionForm form

  HttpServletRequest request

  HttpServletResponse response)

  throws Exception {

  PrintWriter out = responsegetWriter();

  outprint();//ajax取得都是字符的輸出如果數據量大的話還可以用xml來發送和接受

  return null;

  }

  }

  strutsconfigxml

  <action type=testwhwuploadAjaxAction validate=false scope=request   path=/ajax/>

  ----------------------------

  如果是servlet

  webxml

  <servlet>

  <servletname>AjaxServlet </servletname>

  <servletclass>servletAjaxServlet </servletclass>

  </servlet>

  <servletmapping>

  <servletname>AjaxServlet </servletname>

  <urlpattern>/AjaxServlet servlet</urlpattern>

  </servletmapping>

  AjaxServletjava

  import javaioIOException;

  import javaioPrintWriter;

  import javaxservletServletException;

  import javaxservlethttpHttpServlet;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  public class AjaxServlet extends HttpServlet {

  Logger log = LoggergetLogger(thisgetClass());

  public void doGet(HttpServletRequest request HttpServletResponse response)

  throws ServletExceptionIOException {

  responsesetContentType(text/xml; charset=GBK);

  PrintWriter out = responsegetWriter();

  outprint();

  }

  }

  //Process the HTTP Post request

  public void doPost(HttpServletRequest request HttpServletResponse response)

  throws ServletException IOException {

  doGet(request response);

  }

  //Process the HTTP Put request

  public void doPut(HttpServletRequest request HttpServletResponse response)

  throws ServletException IOException {

  }

  //Process the HTTP Delete request

  public void doDelete(HttpServletRequest request

  HttpServletResponse response) throws ServletException IOException {

  }

  //Clean up resources

  public void destroy() {

  }

  }

  -------------------------------------

  如果是webwork

  xworkxml

  <action name=ajax class=comwhwuploadactionwebworkAjaxAction method=ajax/>

  AjaxActionjava

  import javaioPrintWriter;

  public class AjexAlbumAction extends ActionSupport implements Action{

  public void ajax()throws IOException {

  PrintWriter pw = ServletActionContextgetResponse()getWriter();

  ServletActionContextgetResponse()setContentType(text/html;charset=GBK);

  pwprint();

  pwclose();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26032.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.