欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ajax的簡單實用實例代碼

 更新時間:2017年05月05日 15:27:16   投稿:mrr  
這篇文章主要介紹了Ajax的簡單實用實例代碼,需要的朋友可以參考下

我將實現(xiàn)一個簡單的Ajax頁面無刷新進(jìn)行用戶驗證案例:

效果如下圖:

實現(xiàn)主要過程:

在UsersAction類中的checkUser方法中接收并驗證前臺的表單數(shù)據(jù),針對不同情況,返回一個狀態(tài)碼code給jsp頁面,然后在ajax1.jsp中通過$.post方法接受后臺傳遞過來的狀態(tài)碼

做出不同的響應(yīng)。

具體代碼如下:

1.實體類

package com.bean;
import java.io.Serializable;
public class Users implements Serializable {
 private String uname;
 private String passwd;
 public String getUname() {
  return uname;
 }
 public void setUname(String uname) {
  this.uname = uname;
 }
 public String getPasswd() {
  return passwd;
 }
 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }
 public Users(String uname, String passwd) {
  super();
  this.uname = uname;
  this.passwd = passwd;
 }
 public Users() {
  super();
 }
}

2.action類

package com.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import com.bean.Users;
public class UsersAction {
 private Users us;
 public Users getUs() {
  return us;
 }
 public void setUs(Users us) {
  this.us = us;
 }
 @Action(value="checkUser")
 public String checkUser() {
  System.out.println("aaaaaaaaa");
  HttpServletResponse response = ServletActionContext.getResponse();
  response.setCharacterEncoding("utf-8");
  try {
   PrintWriter out = response.getWriter();
   int code = 0;
   if (us == null) {
    out.print(0);
    return null;
   } else {
    if (us.getUname() == null || us.getUname().trim().equals("")) {
     code = 1;
     out.print(code);
     return null;
    } else {
     if (us.getPasswd() == null
       || us.getPasswd().trim().equals("")) {
      code = 2;
      out.print(code);
      return null;
     } else {
      code = 200;
      out.print(code);
     }
    }
   }
   out.flush();
   out.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }
}

3.ajax1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" >
 <title>Ajax練習(xí)</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
 -->
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script>
 $(function() {
  $("#btok").click(function() {
   //獲取數(shù)據(jù)
   var uname = $("#uname").val();
   var passwd = $("#passwd").val();
   //將數(shù)據(jù)組織為json格式
   var json = {"us.uname":uname,"us.passwd":passwd};
   //進(jìn)行異步請求
   $.post("checkUser.action",json,function(msg){
    if(msg == '0') {
     alert("用戶名和密碼錯誤!");
     return;
    }
    if(msg == '1') {
     $("#uerror").html("用戶名錯誤!");
     return;
    } else {
     $("#uerror").html("*");
    }
    if(msg == '2') {
     $("#perror").html("密碼錯誤!");
     return;
    } else {
     $("#perror").html("*");
    }
    if(msg == '200') {
     alert("登陸成功!");
     return;
    }
   });
  });
 });
</script>
 </head>
 <body>
 <form name="form1" method="post" action="">
 <table width="450" border="1" align="center" cellpadding="1" cellspacing="0">
  <tr>
  <td colspan="2" align="center" valign="middle" bgcolor="#FFFFCC">用戶注冊</td>
  </tr>
  <tr>
  <td width="88">賬號:</td>
  <td width="352"><label for="uname"></label>
  <input type="text" name="uname" id="uname">
  <span id="uerror" style="color:#F06;">*</span></td>
  </tr>
  <tr>
  <td>密碼:</td>
  <td><label for="passwd"></label>
  <input type="password" name="passwd" id="passwd">
  <span id="perror" style="color:#F06;">*</span></td>
  </tr>
  <tr align="center" valign="middle" bgcolor="#FFFFCC">
  <td colspan="2"><input type="button" name="button" id="btok" value="確定">
  <input type="reset" name="button2" id="button2" value="重置"></td>
  </tr>
 </table>
 </form>
 <br>
 </body>
</html>

以上所述是小編給大家介紹的Ajax的簡單實用實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 聊一聊Ajax的優(yōu)缺點

    聊一聊Ajax的優(yōu)缺點

    和大家一起聊一聊Ajax的優(yōu)缺點,Ajax是一個動態(tài)的WEB應(yīng)用的開發(fā)技術(shù),它的出現(xiàn)豐富了用戶的體驗,今天就和大家進(jìn)一步的了解Ajax,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 基于Jquery.history解決ajax的前進(jìn)后退問題

    基于Jquery.history解決ajax的前進(jìn)后退問題

    本文主要給大家介紹基于Jquery.history解決ajax的前進(jìn)后退問題,涉及到j(luò)query前進(jìn)后退相關(guān)方面的知識,本文內(nèi)容經(jīng)典,非常具有參考價值,特此把jquery前進(jìn)后退相關(guān)知識分享在腳本之家網(wǎng)站供大家參考
    2015-10-10
  • 使用Jquery+Ajax+Json如何實現(xiàn)分頁顯示附JAVA+JQuery實現(xiàn)異步分頁

    使用Jquery+Ajax+Json如何實現(xiàn)分頁顯示附JAVA+JQuery實現(xiàn)異步分頁

    本文給大家介紹基于jquery+ajax+json實現(xiàn)數(shù)據(jù)分頁顯示,以及JAVA+JQuery實現(xiàn)異步分頁,本文代碼簡單易懂,非常具有參考價值,感興趣的朋友一起學(xué)習(xí)吧
    2015-10-10
  • AJax與Jsonp跨域訪問問題小結(jié)

    AJax與Jsonp跨域訪問問題小結(jié)

    本文給大家匯總介紹了JavaScript的AJax,JQuery的AJax以及利用jsonp實現(xiàn)跨域訪問的問題,非常的細(xì)致全面,有需要的小伙伴可以參考下。
    2015-12-12
  • AJAX的阻塞及跨域名解析

    AJAX的阻塞及跨域名解析

    AJAX的阻塞及跨域名解析...
    2007-07-07
  • ajax session過期問題的幾個解決方案

    ajax session過期問題的幾個解決方案

    現(xiàn)在web開發(fā)中,ajax應(yīng)用的比較多。關(guān)于此方面的框架也不少。在應(yīng)用中都會遇到session過期的問題
    2009-07-07
  • jQuery Ajax方式上傳文件的方法

    jQuery Ajax方式上傳文件的方法

    這篇文章主要介紹了jQuery Ajax方式上傳文件的方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • ASP.NET與Ajax的實現(xiàn)方式小總結(jié)

    ASP.NET與Ajax的實現(xiàn)方式小總結(jié)

    Ajax 應(yīng)該不是一項技術(shù),是一種思想而已,跟 ASP.NET 以及其它 Web 開發(fā)語言沒有什么太大關(guān)系,這里只是談?wù)?ASP.NET 中目前使用的 Ajax 技術(shù)以及其它一些實現(xiàn) Ajax 的優(yōu)秀框架。感興趣的朋友跟著小編一起學(xué)習(xí)asp.net與ajax的實現(xiàn)方式
    2015-09-09
  • 通達(dá)OA 使用Ajax和工作流插件實現(xiàn)根據(jù)人力資源系統(tǒng)數(shù)據(jù)增加OA賬號(圖文詳解)

    通達(dá)OA 使用Ajax和工作流插件實現(xiàn)根據(jù)人力資源系統(tǒng)數(shù)據(jù)增加OA賬號(圖文詳解)

    這篇文章主要介紹了通達(dá)OA 使用Ajax和工作流插件實現(xiàn)根據(jù)人力資源系統(tǒng)數(shù)據(jù)增加OA賬號(圖文詳解),需要的朋友可以參考下
    2016-12-12
  • js ajax加載時的進(jìn)度條代碼

    js ajax加載時的進(jìn)度條代碼

    在web頁面中使用ajax加載進(jìn)度條,可以讓用戶預(yù)先判斷等待的時間,而且還可以減少用戶等待信息加載過程中的心理焦慮感,所以進(jìn)度條加載必不可少,下面小編給大家介紹ajax加載進(jìn)度條代碼是如何實現(xiàn)的
    2015-10-10

最新評論