(二十四):登录及注销 (JSP学习第七天)

登录及注销

主要用Session来实现
用户登录成功后,设置一个Session范围属性,然后在其他需要验证的页面判断是否存在此Session属性范围,存在则表示正常登录
过的合法用户,不存在给予提示,跳转登录页重新登录,登录后再进行注销操作
只要有这几个页面
login.jsp
welcome.jsp
logout.jsp

编写表单并执行验证login.jsp

<%@ page language="java" 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="upass"><br/>
 <input type="submit" value="登录">
 <input type="reset" value="重置">
</form>
<%
 String name=request.getParameter("uname");
 Stirng password=request.getParameter("upass");
 if(!(name==null||"".equals(name)||password==null||"".equals(password))){
 if("esyzf".equals(name)&&"123456".equals(password)){
 response.setHeader("refresh","2;url=welcome.jsp");
 session.setAttribute("userid",name);
%>
<h3>用户登录成功,两秒后跳转到欢迎页面</h3>
<h3>如何没有跳转,请按<a href="welcome.jsp">这里</a></h3>
<%
 }
}
%>
</body>
</html>

欢迎页 welcome.jsp

<%@ page language="java" 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 language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
 response.setHeader("refresh","2;url=login.jsp");//定时跳转
 session.invalidate();//注销
%>
<h3>你好,你已经退出本系统,两秒后跳会首页</h3>
<h3>如没有跳转,请按<a href="login.jsp">这里</a>
</body>
</html>
posted @ 2011-10-07 21:48  techstan  阅读(7642)  评论(2编辑  收藏  举报