基于jQuery实现Ajax验证用户名是否可用实例
本文实例为大家分享了jQuery ajax简单案例-验证用户名是否可用的具体代码,供大家参考,具体内容如下
HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="/jquery_ajax/js/jquery-1.8.3.js"></script> <script type="text/javascript"> //页面加载完成后 $(function() { //添加失焦事件 $("#username").blur(function() { //获取录入的用户名 var usernamevalue = $("#username").val(); //向服务器发送请求 var url="/jquery_ajax/load"; $("#username_span").load(url,{'username':usernamevalue}); }); }); </script> </head> <body> <input type="text" name="username" id="username"><span id="username_span"></span> <br> <input type="password" name="password"> <br> </body> </html>
LoginServlet
public class LoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决乱码 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); // 1.得到用户名 String username = request.getParameter("username"); // 2.判断用户名是否可以使用 if ("tom".equals(username)) { // 用户名不可以使用 response.getWriter().write("<font color='red'>用户名被占用</font>"); } else { // 用户名可以使用 response.getWriter().write("<font color='green'>用户名可以使用</font>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
web.xml
<servlet> <description></description> <display-name>LoadServlet</display-name> <servlet-name>LoadServlet</servlet-name> <!-- Class clazz = Class.forName("com.zxl.servlet.LoadServlet"); Object obj = clazz.newInstatnce(); // 反射去调用 doGet, doPost --> <servlet-class>com.zxl.servlet.LoadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoadServlet</servlet-name> <url-pattern>/load</url-pattern> </servlet-mapping>
相关推荐
胡献根 2020-07-05
troysps 2020-06-13
shayuchaor 2020-06-10
dengweijunkedafu 2020-06-09
Andrew代码战士 2020-06-08
TONIYH 2020-05-25
PlumRain 2020-05-10
Cloudeep 2020-04-15
inhumming 2020-04-10
末点 2020-03-06
hpujsj 2020-03-03
airfling 2020-03-01
ITfooter 2020-02-23
Justdoit00 2020-02-16