加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第一个Ajax程序

发布时间:2020-12-16 00:29:40 所属栏目:百科 来源:网络整理
导读:重点:理解Ajax原理,几大步骤: %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"

重点:理解Ajax原理,几大步骤:

<%@ 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%>">
    
    <title>My JSP 'ajax.jsp' starting page</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">
	
	function createXHR(){        //创建XMLHttpRequest对象
		
		try{
			//alert(1);
			return new ActiveXObject('Msxml2.xmlhttp');
			
		}catch(e){
			//alert(2);

			return new XMLHttpRequest();
			
		}
	}
	//createXHR()
	
	function checkuserexist(){
		
		var username=document.getElementById('userform').username.value;
		//alert(username);
		var xhr=createXHR();                                                       
		
		xhr.onreadystatechange=function(){                //注册处理函数
			if(xhr.readyState==4){
			// 服务器响应正确(当服务器响应正确时,返回值为200的状态码)
				if (xhr.status == 200)
				{
					var msg=xhr.responseText;               //处理响应
					document.getElementById('usermsg').innerHTML='<span style="color:reg">'+msg+'<span>'
				}
			
			}
			
		}
		//建立连接
		xhr.open('GET','index.jsp?u='+username,true);
		//发送请求
		xhr.send(null);
		
		
		//处理响应
	//	xhr.responseText;
		
		
	}
	
	
	</script>

  </head>
  
  <body>
  <form id='userform'>
  请填写用户名:<input type='text' name='username',id='username' onblur='checkuserexist();'/>
  <div id='usermsg'></div>
  </form>
  </body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.setCharacterEncoding("utf-8");
String name=request.getParameter("u");
if(name.equals("abc")){
	out.println("存在");
}else{
	out.println("不存在");

}
out.println("jioijjioj");
out.flush();
%>

 


结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读