环境搭建
导入bootStrap以及JQuery的文件并且引入资源。
在使用bootStrap时必须加入
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
模板一(模态框弹出登录界面)
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>关于css快速总结以及bootStrap使用简介</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/div/userLogin.js"></script>
</head>
<body>
<!-- 按钮触发模态框 -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalLogin">
登录
</button>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModalLogin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h3 class="modal-title" id="myModalLabel">
欢迎登录xx平台
</h3>
</div>
<div class="modal-body">
<form id="loginForm" class="form-horizontal" role="form" action="css.html">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">账号</label>
<div class="col-sm-10">
<input required="required" id="username" name="username" type="text" class="form-control" id="firstname" placeholder="请输入账号...">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input required="required" id="password" name="password" type="text" class="form-control" id="lastname" placeholder="请输入密码...">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input name="flag" type="checkbox">请记住我
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button id="submit" type="submit" class="btn btn-primary col-sm-12">登录</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<a href="#">忘记密码</a>
<a href="#">其他方式登录</a>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
</body>
</html>
js/div/userLogin.js目录下的js文件(添加ajax验证账号是否存在)
$(function () {
//定义两个变量,布尔类型,为判断表单提交
let flag1=false;
let flag2=false;
//验证账号
$("#username").blur(function () {
let reg=/^\w{9,12}$/;
if (reg.test(this.value)){
//ajax判断
$.post("LoginServlet","username="+$(this).val(),function (data) {
if (data.flag){
return true;
}else {
$("#username").attr("style","border: 2px solid red");
$("#username").after("<span id='username_span' style='color: red;font-size: xx-small'>账号不存在...</span>");
}
},"json");
}else{
//添加账号不符合规范的信息
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='username_span' style='color: red;font-size: xx-small'>账号应该为9-12个长度的字符串...</span>");
}
});
//验证密码
$("#password").blur(function () {
let reg=/^\w{6,15}$/;
if (reg.test(this.value)){
flag2 = true;
}else{
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='password_span' style='color: red;font-size: xx-small'>密码应该为6-15个长度字符串...</span>");
}
});
//聚焦时去掉错误提示信息格式
$("#username").focus(function () {
$(this).removeAttr("style");
$("#username_span").remove();
});
$("#password").focus(function () {
$(this).removeAttr("style");
$("#password_span").remove();
});
//提交表单
$("#loginForm").submit(function () {
return flag1&&flag2;
});
});
servlet的响应
package cn.supperbro.servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
Map<String,Object> map = new HashMap<>();
map.put("flag","180591106".equals(username));
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(map);
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(s);
}
}
模板二(登陆,忘记密码,以及其他方式登录)
loginContext.html
<!DOCTYPE html>
<html lang="en" xmlns="">
<head>
<meta charset="UTF-8">
<title>login...</title>
</head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>关于css快速总结以及bootStrap使用简介</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/div/loginContext.js"></script>
<body style="background-color: #e5e5e5">
<div class="container-fluid" style="background-color: white">
<div class="col-sm-12">
<h1> <span class="glyphicon glyphicon-home" aria-hidden="true"></span> xx系统 | <span class="text-success">Login</span></h1>
</div>
</div><br/>
<div class="container" >
<div class="col-sm-9" style="background-color: white;height: 15cm" >
<h2 class="text-center" style="padding-top: 1cm">欢迎登录</h2>
<hr style="border: 1px solid #ebccd1"/>
<br/>
<form id="loginForm" class="form-horizontal" role="form" action="#">
<div class="form-group">
<label for="inputUsername" class="col-sm-2 control-label"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></label>
<div class="col-sm-9">
<input required="required" type="text" class="form-control" id="inputUsername" name="inputUsername"
placeholder="请输入账号...">
</div>
</div><br/>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span class="glyphicon glyphicon-asterisk" aria-hidden="true"></span></label>
<div class="col-sm-9">
<input required="required" type="password" class="form-control" id="inputPassword" name="inputPassword"
placeholder="请输入密码...">
</div>
</div><br/>
<div class="form-group">
<label for="userType" class="col-sm-2 control-label"><span class="glyphicon glyphicon-th-large" aria-hidden="true"></span></label>
<div class="col-sm-9">
<select id="userType" name="userType" class="form-control">
<option selected="selected">学生登录</option>
<option>职工登录</option>
<option>管理员登录</option>
</select>
</div>
</div><br/>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span></span></label>
<div class="col-sm-9">
<input type="submit" class="form-control btn-primary" id="inputSubmit" style="height: 1.2cm" value="登录">
</div>
</div>
<div id="endPage" class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span></span></label>
<div class="col-sm-9">
<input type="checkbox" checked="checked"> 十天免登录 | <a href="findPassword.html">忘记密码</a>
| <a style="cursor: pointer" title="提示"
data-container="body" data-toggle="popover" data-placement="bottom"
data-content="暂时不支持其他方式登录...">
其他方式登录</a>
</div>
</div>
</form>
</div>
<div class="col-sm-3" style="background-color: #adadad;height: 15cm" >
<h3 class="text-success text-center" style="margin-top: 50%">相关资讯...</h3>
</div>
</div>
</body>
</html>
loginContext.js
$(function () {
$("[data-toggle='popover']").popover();
});
$(function () {
//定义两个变量,布尔类型,为判断表单提交
let flag1=false;
let flag2=false;
let flag3=false;
//验证账号
$("#inputUsername").blur(function () {
let reg=/^\w{9,12}$/;
if (reg.test(this.value)){
$.post("LoginServlet","inputUsername="+$(this).val(),function (data) {
if (data.flagUsername){
flag1 = true;
}else {
$("#inputUsername").attr("style","border: 2px solid red");
$("#inputUsername").after("<span id='inputUsername_span' style='color: red;font-size: xx-small'>账号不存在...</span>");
}
},"json");
}else{
//添加账号不符合规范的信息
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputUsername_span' style='color: red;font-size: xx-small'>账号应该为9-12个长度的字符串...</span>");
}
});
//验证密码
$("#inputPassword").blur(function () {
let reg=/^\w{6,15}$/;
if (reg.test(this.value)){
flag2 = true;
}else{
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputPassword_span' style='color: red;font-size: xx-small'>密码应该为6-15个长度字符串...</span>");
}
});
//聚焦时去掉错误提示信息格式
$("#inputUsername").focus(function () {
$(this).removeAttr("style");
$("#inputUsername_span").remove();
});
$("#inputPassword").focus(function () {
$(this).removeAttr("style");
$("#inputPassword_span").remove();
});
//提交表单
$("#loginForm").submit(function () {
//点击提交按钮事件响应
if (flag1&&flag2){
$.post("LoginServlet",$("#loginForm").serialize(),function (data) {
if (data.flagPassword){
alert('登录成功');
return true;
}else {
$("#endPage").after("<div id=\"alertInfo\" class=\"alert alert-danger text-center col-sm-7 col-sm-offset-3\">密码错误,请重新输入...</div>");
setTimeout(function () {
$("#alertInfo").remove();
},3000);
}
},"json");
}else {
return flag3;
}
});
});
findPassword.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>findPassword...</title>
</head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>关于css快速总结以及bootStrap使用简介</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/div/findPassword.js"></script>
<body style="background-color: #e5e5e5">
<div class="container-fluid" style="background-color: white">
<div class="col-sm-12">
<h2> <span class="glyphicon glyphicon-home" aria-hidden="true"></span> xx系统 | <span class="text-success">findPassword</span></h2>
</div>
</div><br/>
<div class="container" >
<div class="col-sm-10 col-sm-offset-1" style="background-color: white;height: 18cm" >
<h2 class="text-center" style="padding-top: 1cm">找回密码</h2>
<hr style="border: 1px solid #ebccd1"/>
<br/>
<form class="form-horizontal" role="form" id="findPasswordForm" action="#">
<div class="form-group">
<label for="inputUsername" class="col-sm-2 control-label"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></label>
<div class="col-sm-7">
<input required="required" type="text" class="form-control" id="inputUsername" name="inputUsername"
placeholder="请输入账号...">
</div>
</div><br/>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></label>
<div class="col-sm-7">
<input required="required" type="text" class="form-control" id="inputEmail" name="inputEmail"
placeholder="请输入邮箱...">
</div>
</div><br/>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span class="glyphicon glyphicon-asterisk" aria-hidden="true"></span></label>
<div class="col-sm-7">
<input required="required" type="password" class="form-control" id="inputPassword" name="inputPassword"
placeholder="请输入密码...">
</div>
</div><br/>
<div class="form-group">
<label for="inputPasswordAgain" class="col-sm-2 control-label"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></label>
<div class="col-sm-7">
<input required="required" type="password" class="form-control" id="inputPasswordAgain" name="inputPasswordAgain"
placeholder="再次输入密码...">
</div>
</div><br/>
<div class="form-group">
<label for="inputCode" class="col-sm-2 control-label"><span class="glyphicon glyphicon-globe" aria-hidden="true"></span></label>
<div class="col-sm-5">
<input required="required" type="text" class="form-control" id="inputCode" name="code"
placeholder="请输入验证码...">
</div>
<div class="col-sm-2">
<input id="getCode" type="button" class="form-control btn btn-info" value="验证码">
</div>
</div><br/>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span></span></label>
<div class="col-sm-7">
<input type="submit" class="form-control btn-primary" id="inputSubmit" style="height: 1.2cm" value="修改密码">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label"><span></span></label>
<div class="col-sm-7">
<a href="loginContext.html" target="_self">返回登录</a>
| <a style="cursor: pointer" title="提示"
data-container="body" data-toggle="popover" data-placement="bottom"
data-content="暂时不支持其他方式登录...">
其他方式登录</a>
</div>
</div>
</form>
</div>
</div>
<script>
$(function () {
$("[data-toggle='popover']").popover();
});
</script>
</body>
</html>
findPassword.js
$(function () {
$("[data-toggle='popover']").popover();
});
//bootStrap弹出框
$(function () {
//定义变量,布尔类型,为判断表单提交
let flag1=false;
let flag2=false;
let flag3=false;
let flag4=false;
//验证账号
$("#inputUsername").blur(function () {
let reg=/^\w{9,12}$/;
if (reg.test(this.value)){
flag1 = true;
}else{
//添加账号不符合规范的信息
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputUsername_span' style='color: red;font-size: xx-small'>账号应该为9-12个长度的字符串...</span>");
flag1=false;
}
});
//验证密码
$("#inputPassword").blur(function () {
let reg=/^\w{6,15}$/;
if (reg.test(this.value)){
flag2 = true;
}else{
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputPassword_span' style='color: red;font-size: xx-small'>密码应该为6-15个长度字符串...</span>");
flag2=false;
}
});
//验证第二次密码是否一致
$("#inputPasswordAgain").blur(function () {
let first = $("#inputPassword").val();
let second = $("#inputPasswordAgain").val();
if (first==second){
flag3=true;
}else {
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputPasswordAgain_span' style='color: red;font-size: xx-small'>两次密码输入不一致...</span>");
flag3=false;
}
});
//验证邮箱格式是否正确
$("#inputEmail").blur(function () {
let reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
flag4 = reg.test(this.value);
if (flag4){}else {
$(this).attr("style","border: 2px solid red");
$(this).after("<span id='inputEmail_span' style='color: red;font-size: xx-small'>请输入正确的邮箱...</span>");
flag4=false;
}
});
//聚焦时去掉错误提示信息格式
$("#inputUsername").focus(function () {
$(this).removeAttr("style");
$("#inputUsername_span").remove();
});
$("#inputPassword").focus(function () {
$(this).removeAttr("style");
$("#inputPassword_span").remove();
});
$("#inputPasswordAgain").focus(function () {
$(this).removeAttr("style");
$("#inputPasswordAgain_span").remove();
});
$("#inputEmail").focus(function () {
$(this).removeAttr("style");
$("#inputEmail_span").remove();
});
//获取验证码
$("#getCode").click(function () {
//使得验证码按钮失效60秒
$(this).prop("disabled",true);
let num = 61;
let id =setInterval(function () {
num--;
$("#getCode").val("重新发送("+num+")");
if (num<1){
clear();
}
},1000);
function clear() {
clearInterval(id);
$("#getCode").val("验证码");
$("#getCode").prop("disabled",false);
}
$.get("FindPasswordServlet","flag=true",function (data) {
},"json");
});
//提交表单
$("#findPasswordForm").submit(function () {
//点击提交按钮事件响应
if (flag1&&flag2&&flag3&&flag4){
let f1 = false;
let f2 = false;
$.post("LoginServlet",$("#loginForm").serialize(),function (data) {
//验证验证码是否一致
if (data.flagCode){
f1=true;
}else {
}
//验证账号与邮箱是否对应
if(data.flagUser){
f2=true;
}
else {
$("#endPage").after("<div id=\"alertInfo\" class=\"alert alert-danger text-center col-sm-7 col-sm-offset-3\">账号与邮箱不对应...</div>");
setTimeout(function () {
$("#alertInfo").remove();
},3000);
}
},"json");
return f1&&f2;
}else {
return false;
}
});
});