本文的ajax技术用jquery实现。后台用servlet。
本程序的实现了异步检测用户名在数据库是否已经存在的功能。
第一步:
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户注册</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"> --> <script type="text/javascript" src="<%=path %>/js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="<%=path %>/js/index.js"></script> </head> <body> <p>欢迎登陆</p> <form> 用户名:<input type="text" name="userName" id="userName"><span id="userNameHint"></span><br> 密 码:<input type="password"><br> <input type="submit" value="注册"> </form> </body> </html>
一个简单的form表单。一部检测id为userName的输入框。使用id为userNameHit的span来返回提示。
第二步:
js:(注:首先要引入jquery)
$(document).ready(function() { $("#userName").change(function(){ $.ajax({ url:'RegisterSer', data:{userName:$("#userName").val()}, type: "POST", error:function(){ alert("error occured!!!"); }, success:function(data){ if(data=="true"){ $("#userNameHint").text("恭喜!账号可用").css({"color":"green"}); }else{ $("#userNameHint").text("抱歉!该账号已存在").css({"color":"green"}); } } }) }); });
第三步:
新建servlet:RegisterSer
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("hellworld"); String userName = request.getParameter("userName"); if (userName.equals("exist")) { response.getWriter().write("false"); } else { response.getWriter().write("true"); } }
使用response来返回信息给前台。
相关推荐
Ajax的HelloWorld1
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/826701
Ajax实现helloworld!实例,eclipse+struts+Ajax初学者必看
关于ajax hello world的代码
ajax例子----Hello world
JSF2.0实战 - 1-3、Hello World,AJAX体验,onPageLoad示例代码
pear html_ajax实例
资源名称:Vaadin简介和HelloWorld 中文WORD版内容简介:本文档主要讲述的是Vaadin简介和HelloWorld;Vaadin(芬兰民间传说中的一种神话生物,是女神也是一种山岭驯鹿的神话祖先) 是一款使用 Apache V2 许可协议的...
第一个Rap程序。 (RAP - Rich Ajax Platform )
基于Spring MVC 2.5 和JQuery 实现的Ajax的一个HelloWorld例子,可以参照这个例子在其他Spring MVC工程加入ajax功能
Struts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例...helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworld
ajax调用java实例源码Lightstreamer - “Hello World”教程 - HTML 客户端 “Hello World with Lightstreamer”系列的演示是非常基本的示例,我们将交替的字符串“Hello”和“World”,后跟当前时间戳,从服务器推送...
因此,我将在书中讲述ASP.NET AJAX的方方面面,从最基础的“Hello World”示例到复杂的整站程序和底层的实现原理分析,从最简单的客户端效果到其深层次的用户心理学分析及用户体验改善。 正因为本书的介绍范围广泛...
HelloWorld 第一个Ajax应用程序 第2章 浏览器中的JavaScript Test4Navigator.htm User-Agent检测法 Test4Window1.htm window对象属性 test4Alert.htm 系统对话框 Test4Timeout.htm 时间间隔和暂停 Test...
电子书,让大家轻轻松松掌握ajax的开发
本文展示了AngularJS框架实现的hello world代码示例. 如下是一些你在看Hello World 示例和接下来的代码示例时需要重点关注的方面. ng-app, ng-controller, ng-model 指令 带有两个大括弧的模板 步骤 1: 在<Head...
仿google、百度的收入提示框,但有所不同,还有待完善。代码简单易懂
Hello World!分析 完善的AJAX应用程序:AJAX留言板 调试技巧 常见问题
我们经常会使用JavaScript实现动态的改变div里面的内容,尤其是使用ajax的时候,尤为重要。