使用Ajax做登录验证

使用Ajax做登录验证

  1. html
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="JS/jQuery.js"></script>
<style type="text/css">
table {
 width: 360px;
 height: 45px:    
 text-align: center;
 margin-top: 120px;
 border-collapse: collapse;
}
input {
 width: 280px;
 height: 30px;
}
</style>
</head>
<body>
 <form action="#" method="post">
  <center>
   <table align="center" border="1">
    <tr>
     <td>用户名:</td>
     <td><input type="text" name="name" id="username" onblur="verifyName()" /></td>
    </tr>
    <tr>
     <td>密码:</td>
     <td><input type="text" name="pwd" id="mypwd" onblur="verifyPwd()" /></td>
    </tr>
    <tr>
     <td colspan="3" align="center" height="36px">
     <input type="button" style="width: 8rem;height:27px" value="提交登录验证" /></td>
    </tr>
   </table>
  </center>
 </form>
</body>
</html>
  1. js
<script type="text/javascript">
  function verifyName() {//用户名校验
   var verifyName = document.getElementById("username").value;
   var name = /^[A-Z][0-9A-Za-z_][a-zA-Z0-9_]{5,19}$/; // 大写字母开头 6-20位字符(不允许有符号但是允许有_)
   if (!name.test(verifyName)) {
    $("#username").css("border-color", "red");
    return false;
   } else {
    return true;
   }
  }
  function verifyPwd() {//密码
   var verifyPwd = document.getElementById("mypwd").value;
   var pwd = /^[A-Z][A-Za-z0-9]\w{7,14}.{1,20}$/; //大写开头 数字字母符号混合 8-15位
   if (!pwd.test(verifyPwd)) {
    $("#username").css("border-color", "red");
    return false;
   } else {
    return true;
   }
  }
  $(function() {
   $(":button").on("click", function() {
    $.ajax({
     type : "post",
     url : "AJAXServlet",
     data : {
      name : $("#username").val(),
      pwd : $("#mypwd").val()
     },
     dataType : "text",
     success : function(data) {
      if (data == "ok") {
       window.location.href = "show.jsp";
      } else {
       alert("登录失败!");
       $("#mypwd").val("");
       $("#username").focus().select();
      }
     }
    });
   });
  });
 </script>
  1. servlet
package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  
  String name = "张三";
  String pwd = "123456";
  String ajaxName = request.getParameter("name");
  String ajaxPwd = request.getParameter("pwd");
  System.out.println(ajaxName + "——" + ajaxPwd);
  if (name.equals(ajaxName) && pwd.equals(ajaxPwd)) {
   out.print("ok");
  } else {
   out.print("error");
  }
  out.flush();
  out.close();
 }
}

4.效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值