ajax简单实例用户名验证

.java

package com.atguigu.ajax.app.servlets;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ValiateUserNameServlet
 */
@WebServlet("/valiateUserName")
public class ValiateUserNameServlet extends HttpServlet {
<span style="white-space:pre">	</span>private static final long serialVersionUID = 1L;


<span style="white-space:pre">	</span>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<span style="white-space:pre">		</span>List<String> usernames = Arrays.asList("aa","BB","CC");
<span style="white-space:pre">		</span>String userName = request.getParameter("userName");
<span style="white-space:pre">		</span>String re = null;
<span style="white-space:pre">		</span>if(usernames.contains(userName)){
<span style="white-space:pre">			</span>re = "<font color='red'> wrong! </font>";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>else{
<span style="white-space:pre">			</span>re = "<font color='green'> success! </font>";
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>response.setCharacterEncoding("UTF-8");
<span style="white-space:pre">		</span>response.setContentType("text/html");
<span style="white-space:pre">		</span>response.getWriter().print(re);
<span style="white-space:pre">	</span>}


}


jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<!-- 
执行时间不同: $(document).ready在页面框架下载完毕后就执行;而window.onload必须
在页面全部加载完毕(包含图片下载)后才能执行。很明显,前者的执行效率高于后者。
  执行数量不同: $(document).ready可以重复写多个,并且每次执行结果不同;而window.
onload尽管可以执行多个,但仅输出最后一个执行结果,无法完成多个结果的输出。
  $(document).ready(function(){}) 可以简写成 $(function(){}),因此与下面的代码是等
价的。
 -->
 <!-- 
 1.导入jquery库
 2、获取节点
 3.添加响应函数
 3.1获取username的value属性值,准备发送ajax请求
 3.2发送ajax请求检验username是否可用
 3.3在服务器直接返回一个html的片段进行提示
 3.4返回信息显示
  -->
  <script type="text/javascript" src="${pageContext.request.contextPath}/jquery.js"></script>
  
  <script type="text/javascript">
  $(function() {
		$(":input[name='username']").change(function(){
			alert("aa");
			var val = $(this).val();
			val = $.trim(val);
			//alert(val);
			
			if(val != ""){
				var url = "${pageContext.request.contextPath}/valiateUserName";
				var args = {"userName":val,"time":new Date()};
				//alert(url);
				$.post(url,args,function(data){
					$("#message").html(data);
				});
			}//alert(val);
		});
	})
  </script>
<body>
<form method="post" action="">
usernmae:<input type="text" name="username">
<div id="message">

</div>
<input type="submit" value="onsubmit">
</form>
</body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值