效果图:
代码:
<script 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
style="background: url('tupian/1.png');background-size:100% 100% ; background-attachment: fixed">
<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").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)) {
$("#mypwd").css("border-color", "red");
return false;
} else {
return true;
}
}
$(function() {
$(":button").on("click", function() {
$.ajax({
type : "post",
url : "servlet/AjaxServlet",
data : {
name : $("#username").val(),
pwd : $("#mypwd").val()
},
dataType : "text",
success : function(data) {
if (data == "ok") {
window.location.href = "show.jsp";
} else {
alert("登陆失败!!!")
}
}
});
});
});
</script>
</body>
跳转页面代码:
<body>
来了!老弟 <br>
</body>
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 showName = request.getParameter("name");
String showPwd = request.getParameter("pwd");
System.out.println(showName +":" + showPwd);
String name = "Abc123456";
String pwd = "Abc123456789";
if(name.equals(showName) && pwd.equals(showPwd)){
out.print("ok");
}
out.flush();
out.close();
}
}
背景图: