我們來分一下步驟吧:
1.HTML代碼,頁面先寫出來;
2.正則表達(dá)式驗證輸入的用戶名密碼是否正確,失去焦點驗證
3.Ajax異步提交
4.servlet這是后臺處理代碼獲取數(shù)據(jù)并對比響應(yīng),然后跳轉(zhuǎn)成功頁面
效果圖:
結(jié)構(gòu):
代碼如下:
%@ 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>
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").after("span>大寫字母開頭6-20位字符(不允許有符號但是允許有_)/span>");
$("#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}$/; //大寫開頭 數(shù)字字母符號混合 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>
/body>
/html>
servlet代碼:
package com.chaz.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 {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name = "ZhangSan";
String pwd = "Zhang123456";
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();
}
}
web.xml:
?xml version="1.0" encoding="UTF-8"?>
web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
servlet>
description>This is the description of my J2EE component/description>
display-name>This is the display name of my J2EE component/display-name>
servlet-name>AJAXServlet/servlet-name>
servlet-class>com.chaz.servlet.AJAXServlet/servlet-class>
/servlet>
servlet-mapping>
servlet-name>AJAXServlet/servlet-name>
url-pattern>/AJAXServlet/url-pattern>
/servlet-mapping>
/web-app>
跳轉(zhuǎn)成功頁面就這個😄:
body> 登錄成功!/body>
總結(jié)
以上所述是小編給大家介紹的使用AJAX(包含正則表達(dá)式)驗證用戶登錄的步驟,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:- Ajax和PHP正則表達(dá)式驗證表單及驗證碼
- Java使用正則表達(dá)式驗證手機(jī)號和電話號碼的方法
- JS中驗證整數(shù)和小數(shù)的正則表達(dá)式
- Android 2018最新手機(jī)號驗證正則表達(dá)式方法
- 微信小程序?qū)崿F(xiàn)簡單input正則表達(dá)式驗證功能示例
- 基于jQuery實現(xiàn)的Ajax 驗證用戶名唯一性實例代碼
- Ajax驗證用戶名或昵稱是否已被注冊
- php傳值方式和ajax的驗證功能
- 用AJAX實現(xiàn)頁面登陸以及注冊用戶名驗證的簡單實例