<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Task04-login</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">
</head>
<body>
<!--传值到"login_action.jsp" 方式是post隐式传递。-->
<form action="/FridayJob/Task/Task04/login_action.jsp" method="post">
<!-- 通过正则表达式决定输入的字符 -->
用户名:<input pattern="[a-zA-Z0-9]{6,12}" name="username">(只能由字母数字组成,长度在6-12位之间)<br />
密码:<input pattern="[a-zA-Z0-9]{6,12}" type="password" name="password">(只能由字母数字组成,长度在6-15位之间)<br /> <input
type="submit" value="登录"><br>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Task04-login_action</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">
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
%>
<!-- 判断字符串是否为空,为空就转到最初的登录页面 -->
<%
if (username.equals("") && password.equals("")) {
response.setHeader("refresh",
"3;URL=/FridayJob/Task/Task04/login.jsp");//这里的3,是你要确定的时间秒 URL是要跳转的地址
%><font color="red" size="5"> 您的登录信息为空<br> 三秒后将跳转到登录页面 <br>
如果没有跳转,请点击 <a href="/FridayJob/Task/Task04/login.jsp">这里</a>!!! <br>
</font>
<%
} else {
out.print("用户名:" + username);
%><br>
<%
out.print("密码:" + password);
%><br>
<%
if (username != null) {
if (username.equals("sweety") && password.equals("123456")) {
out.print("welcome!!!");
} else {
response.setHeader("refresh",
"3;URL=/FridayJob/Task/Task04/login.jsp");
%><font color="red" size="5"> 您的登录信息有误<br> 三秒后将跳转到登录页面 <br>
如果没有跳转,请点击 <a href="/FridayJob/Task/Task04/login.jsp">这里</a>!!! <br>
</font>
<%
}
}
}
%>
</body>
</html>
输入内容为空的话,会自动跳转的
错误的话,也会自动跳转到登录页面
成功