User-Profile-Image
hankin
  • 5
  • Java
  • Kotlin
  • Spring
  • Web
  • SQL
  • MegaData
  • More
  • Experience
  • Enamiĝu al vi
  • 分类
    • Zuul
    • Zookeeper
    • XML
    • WebSocket
    • Web Notes
    • Web
    • Vue
    • Thymeleaf
    • SQL Server
    • SQL Notes
    • SQL
    • SpringSecurity
    • SpringMVC
    • SpringJPA
    • SpringCloud
    • SpringBoot
    • Spring Notes
    • Spring
    • Servlet
    • Ribbon
    • Redis
    • RabbitMQ
    • Python
    • PostgreSQL
    • OAuth2
    • NOSQL
    • Netty
    • MySQL
    • MyBatis
    • More
    • MinIO
    • MegaData
    • Maven
    • LoadBalancer
    • Kotlin Notes
    • Kotlin
    • Kafka
    • jQuery
    • JavaScript
    • Java Notes
    • Java
    • Hystrix
    • Git
    • Gateway
    • Freemarker
    • Feign
    • Eureka
    • ElasticSearch
    • Docker
    • Consul
    • Ajax
    • ActiveMQ
  • 页面
    • 归档
    • 摘要
    • 杂图
    • 问题随笔
  • 友链
    • Spring Cloud Alibaba
    • Spring Cloud Alibaba - 指南
    • Spring Cloud
    • Nacos
    • Docker
    • ElasticSearch
    • Kotlin中文版
    • Kotlin易百
    • KotlinWeb3
    • KotlinNhooo
    • 前端开源搜索
    • Ktorm ORM
    • Ktorm-KSP
    • Ebean ORM
    • Maven
    • 江南一点雨
    • 江南国际站
    • 设计模式
    • 熊猫大佬
    • java学习
    • kotlin函数查询
    • Istio 服务网格
    • istio
    • Ktor 异步 Web 框架
    • PostGis
    • kuangstudy
    • 源码地图
    • it教程吧
    • Arthas-JVM调优
    • Electron
    • bugstack虫洞栈
    • github大佬宝典
    • Sa-Token
    • 前端技术胖
    • bennyhuo-Kt大佬
    • Rickiyang博客
    • 李大辉大佬博客
    • KOIN
    • SQLDelight
    • Exposed-Kt-ORM
    • Javalin—Web 框架
    • http4k—HTTP包
    • 爱威尔大佬
    • 小土豆
    • 小胖哥安全框架
    • 负雪明烛刷题
    • Kotlin-FP-Arrow
    • Lua参考手册
    • 美团文章
    • Java 全栈知识体系
    • 尼恩架构师学习
    • 现代 JavaScript 教程
    • GO相关文档
    • Go学习导航
    • GoCN社区
    • GO极客兔兔-案例
    • 讯飞星火GPT
    • Hollis博客
    • PostgreSQL德哥
    • 优质博客推荐
    • 半兽人大佬
    • 系列教程
    • PostgreSQL文章
    • 云原生资料库
    • 并发博客大佬
Help?

Please contact us on our email for need any support

Support
    首页   ›   Web   ›   Ajax   ›   正文
Ajax

Ajax——用户注册

2020-03-07 19:16:01
851  0 0

阅读完需:约 2 分钟

jsp页面:

<body>
   <h1> 演示用户是否被注册</h1>
   <form action="" method="post">
	用户名:<input type="text" name="username" id="usernameEle"><span id="error"></span><br/>
	密   码 :<input type="password" name="password" id="password"><br/>
	<input type="submit" value="注册">
   </form>
  </body>

jsp页面中的ajax配置

	<script type="text/javascript">
	function createXMLHttpRequest(){
	try {
		return new XMLHttpRequest();//大多数游览器
	} catch (e) {
		try {
			return ActvieXObject("Msxml2.XMLHTTP");//IE6.0
		} catch (e) {
			try {
				return ActvieXObject("Microsoft.XMLHTTP");//IE5.5以及更早
			} catch (e) {
				alert("错误");
				throw e;
			}
		}
	}
	
}
	
		window.onload = function(){
			//获取文本框,给它的失去焦点事件注册监听
			var userEle=document.getElementById("usernameEle")//方法可返回对拥有指定 ID 的第一个对象的引用
			userEle.onblur=function(){
				//1.得到异步对象
				var xmlHttp=createXMLHttpRequest();
				//2.打开链接
				xmlHttp.open("POST","servlet2",true);
				//3.设置请求头 Content-Type
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				//4.发送
				xmlHttp.send("username=" + userEle.value);
				//5.给xmlHttp的onreadystatechange事件注册监听器
				xmlHttp.onreadystatechange=function(){
					if(xmlHttp.readyState==4 && xmlHttp.status==200){
						//获取服务器的响应,判断是否为1
						//是,获取spen,添加“已注册”
						
						var text=xmlHttp.responseText;//返回结果responseText中是JSON的数据
					var span=document.getElementById("error");//获取元素的ID
						if(text == "1"){
							//var span=document.getElementById("error");//获取元素的ID
							span.innerHTML="用户已存在";//在htmld的标签之间写内容
						}else{
							span.innerHTML="";
							
						}
					}
				}
			}
		}
	
	//http://laptop-hi4k4ge3:8080/web1/ajax3.jsp

Servlet配置

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");//设置字符编码
		response.setContentType("text/html;charset=UTF-8");//设置内容 类型
		
		String username=request.getParameter("username");//获取参数
		System.out.println(username);
		if(username.equalsIgnoreCase("asd")){
			//equalsIgnoreCase  将此 String 与另一个 String 进行比较,不考虑大小写。如果两个字符串的长度相等,并且两个字符串中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
	}

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
Enamiĝu al vi
不要为明天忧虑.因为明天自有明天的忧虑.一天的难处一天当就够了。
543文章 68评论 294点赞 594105浏览

随机文章
SpringBoot—SpringSecurity(基于数据库的动态权限配置)
5年前
Spring—WebClient使用
2年前
SpringSecurity—从子线程获取用户登录信息
4年前
Java—定时任务—HashedWheelTimer时间轮
2年前
EasyExcel使用详解
5年前
博客统计
  • 日志总数:543 篇
  • 评论数目:68 条
  • 建站日期:2020-03-06
  • 运行天数:1927 天
  • 标签总数:23 个
  • 最后更新:2024-12-20
Copyright © 2025 网站备案号: 浙ICP备20017730号 身体没有灵魂是死的,信心没有行为也是死的。
主页
页面
  • 归档
  • 摘要
  • 杂图
  • 问题随笔
博主
Enamiĝu al vi
Enamiĝu al vi 管理员
To be, or not to be
543 文章 68 评论 594105 浏览
测试
测试
看板娘
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付