登陆页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="jquery.js"></script>
<style type="text/css">
#name {
width: 250px;
height: 30px;
}
#pwd {
width: 250px;
height: 30px;
}
#marjin{
margin-top: 20%;
}
</style>
</head>
<body>
<div id="color">
<center>
<form action="#">
<div id="marjin">
<b>账号:</b><input type="text" name="name" id="name" /><span
id="nameResult"></span><br> <br> <b>密码:</b><input
type="text" name="pwd" id="pwd" /><span id="pwdResult"></span><br>
<br> <input type="button" value="登录" onclick="verify()"
style="width: 130px;height: 30px;" />
</div>
</form>
</center>
<div id="show"></div>
<script>
$(function() {
$(":button").on("click", function() {
$.ajax({
type : "post",
url : "AjaxServlet",
data : {
name : $("#name").val(),
pwd : $("#pwd").val(),
},
dataType : "text",
success : function(data) {
if (data == "ok") {
window.location.href = "show.jsp";
} else {
$("#show").text("登录失败");
}
}
});
});
});
function verify() {
var name = document.getElementById("name").value;
var pwd = document.getElementById("pwd").value;
//用户名: 验证只能输入汉字
var nameL = /^[\u4e00-\u9fa5]/;
//密码:验证只能输入数字6-12位数字和字母
var pwdL = /^[a-zA-Z0-9]{6,12}$/;
var result = nameL.test(name.trim());
var result1 = pwdL.test(pwd.trim());
if (result) {
document.getElementById("nameResult").innerText = "✔";
document.getElementById("nameResult").style.color = "green";
} else {
document.getElementById("nameResult").innerText = "✘";
document.getElementById("nameResult").style.color = "red";
}
if (result1) {
document.getElementById("pwdResult").innerHTML = "✔";
document.getElementById("pwdResult").style.color = "green";
} else {
document.getElementById("pwdResult").innerText = "✘";
document.getElementById("pwdResult").style.color = "red";
}
}
</script>
</div>
</body>
</html>
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();
}
}
登陆成功页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div style="margin-top: 30%;margin-left: 50%;"><font size="20">登陆成功</font></div>
</body>
</html>