JavaWeb-登录的验证与自动跳转到登录页面

<%@ 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>

输入内容为空的话,会自动跳转的

错误的话,也会自动跳转到登录页面

成功

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值