验证操作顺序:
提交属性
取得属性
判断(空空判断 -->匹配 --> 成功--> 跳转欢迎页)否则()
<%@ page contentType = "text/html" pageEncoding= "GBK"%>
<html>
<head><title>登陆验证</title></head>
<body>
<form action = "login.jsp" method = "post">
用户名:<input type ="text" name = "uname"><br>
密 码:<input type = "password" name = "upassword"><br>
<input type = "submit" value ="提交">
<input type = "reset" value ="重置">
</form>
<%
String name = request.getParameter("uname");
String password = request.getParameter("upassword");
if (!(name==null || "".equals(name)
|| password==null || "".equals(password))){
if ("zhangze".equals(name) && "lhj".equals(password)){
response.setHeader("refresh", "3; URL = welcome.jsp");
session.setAttribute("userid",name);
%>
<h3>登录成功,三秒后跳转到欢迎页!</h3>
<h3>如果浏览器无法跳转,请点击<a href="welcome.jsp"></a>这里</h3>
<%
}else {
%>
<h3>错误的用户名或密码</h3>
<%
}
}
%>
</body>
</html>
欢迎页welcome.jsp
<%@ page contentType ="text/html" pageEncoding ="GBK"%>
<html>
<head>
<title>欢迎页!</title>
</head>
<body>
<%
if(session.getAttribute("userid")!=null){
%>
<h3>欢迎<%=session.getAttribute("userid")%>登录本欢迎页,<a href="logout.jsp">注销</a></h3>
<%
}else {
%>
<h3>请进行系统的的<a href="login.jsp">登录</a></h3>
<%
}
%>
</body>
</html>
注销页logout.jsp
<%@ page contentType ="text/html" pageEncoding ="GBK"%>
<html>
<head>
<title>注销页</title>
</head>
<body>
<%
response.setHeader("refresh", "3; URL = welcome.jsp");
session.invalidate();
%>
<h3>您已经退出本系统,两秒后回到系统首页</h3>
<h3>如果没有跳转,请点击<a href="login.jsp"></a>这里</h3>
</body>
</html>
登录首页:
成功登录后的欢迎页:
注意问题:
1、登录表单属性提交到自己本页面
2、未加上“空空”判断后加载首页时就会出现非法用户错误登录:
3、跳转页问题要考虑浏览器无法跳转问题,而采用手工超链接跳转。