使用Session对象实现页面的登录以及注销功能

一.代码如下:

1.登录页面:login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.jsp" method="post">
用户名:<input type "text" name="uname"/><br/>&nbsp;&nbsp;:
<input type="password" name="upass" /><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>


<%//默认的账号为:muzhi ,密码为:xiaoganrenye
String name=request.getParameter("uname");
String password=request.getParameter("upass");
if(!(name==null||"".equals(name)||password==null||"".equals(password))){
	if("muzhi".equals(name)&&"xiaoganrenye".equals(password)){
		response.setHeader("refresh","2;URL=welcome.jsp");//定时跳转到欢迎页面
		session.setAttribute("userid",name);
	
%>

<h3>用户登录成功,两秒后跳转到欢迎页</h3>
<h3>如果没有跳转,请按<a href="welcome.jsp">跳转</a></h3>
<%}else{ %>
<h3>错误的用户名或密码!!</h3>
<%
}
	}
%>
</body>
</html>

2.欢迎页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</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>

3.注销成功页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setHeader("refresh","2;URL=login.jsp");
session.invalidate();
%>
<h3>您已成功注销,两秒后跳转回首面</h3>
</body>
</html>

结果:
在这里插入图片描述

发布了73 篇原创文章 · 获赞 1 · 访问量 2441

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/105148179
今日推荐