需求:輸入使用者名稱判斷是否存在此使用者
建立表單index.jsp
<%-- Created by IntelliJ IDEA. User: pc Date: 17-4-21 Time: 下午5:15 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>註冊資訊</title> <script type='text/javascript' src='../js/jquery.min.js' charset='utf-8'></script> <style> .top { margin-left: 800px; margin-top: 200px; } </style> <script type="text/javascript"> $(function () { $(":input[name='username']").change(function () { var name = $(this).val(); name = $.trim(name);//取出前後空格 if (name != "") { var url = "/Login_AServlet"; var args = {"username": name, "time": new Date()}; $.get(url, args, function (data) { $(".message").html(data) }); } }); }); </script></head><body><div class="top"> <h1>資訊註冊</h1> <form action="#" method="post"> 使用者名稱:<input type="text" name="username"><span class="message"> </span><br> 郵 箱:<input type="text" name="emile"><br> 性 別:<input type="radio" name="sex" value="男生" checked="checked">男生 <input type="radio" name="sex" value="女生">女生<br> <input type="submit" value="登 錄"> </form></div></body></html>
Login_AServlet.java
<%-- Created by IntelliJ IDEA. User: pc Date: 17-4-21 Time: 下午12:10 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <script type='text/javascript' src='./js/jquery.min.js' charset='utf-8'></script> <title>Title</title> <script type="text/javascript"> $(function () { $("a").click(function () { var url = this.href; var time = {"time": new Date()} $.post(url, time, function (data) { var name = $(data).find("name").text(); var age = $(data).find("age").text(); var email = $(data).find("email").text(); $(".html_").html(""); $(".html_").append("<h2>姓名:" + name + "性別:" + age + "郵箱:" + email); }); return false; }); }); </script> <title>Title</title></head><body><h1>學生資訊:</h1><ul> <li><a href="./xml_ajax/huan.xml">馬小歡</a></li> <li><a href="./xml_ajax/yun.xml">趙雲</a></li> <li><a href="./xml_ajax/ming.xml">小明</a></li></ul><h1 class="html_"></h1></body></html>
文章文集:JavaEE–學習筆記