首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >jsp课程笔记之session(二)

jsp课程笔记之session(二)

作者头像
兮动人
发布2021-06-11 11:17:44
发布2021-06-11 11:17:44
7960
举报
文章被收录于专栏:兮动人的博客兮动人的博客

注销session及共享session案例

login.jsp

代码语言:javascript
复制
<%@ 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="check.jsp" method="post">
		用户名:<input type="text" name="uname"><br/>
		密码:<input type="password" name="upwd"><br/>
		<input type="submit" value="登陆"><br/>
	</form>
</body>
</html>

check.jsp

代码语言:javascript
复制
<%@ 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>
		<%
			request.setCharacterEncoding("utf-8") ;
			String name = request.getParameter("uname");
			String pwd = request.getParameter("upwd");
			if(name.equals("zs") && pwd.equals("abc")){//假设 zs abc
				//只有登录成功,session中才会存在uname /upwd
				session.setAttribute("uname", name)		 ;
				session.setAttribute("upwd", pwd)		;
				System.out.println("sessionId"+session.getId());
				
				//Cookie cookie = new Cookie("uname" ,namxe);
				//response.addCookie(cookie) ;
				//服务端在第一次响应客户端时,会发送一个 JSESSIONID的cookie
				
				//session.setMaxInactiveInterval(10) ;
				
				request.getRequestDispatcher("welcome.jsp").forward(request, response) ;
				
			
			}else{
				//登录失败
				response.sendRedirect("login.jsp") ;
			}
		
		%>
</body>
</html>

welocame.jsp

代码语言:javascript
复制
<%@ 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>
	欢迎您:
		<%
			String name = (String)session.getAttribute("uname") ;
			//如果 用户没有登录,而是直接 通过地址栏 访问welcome.jsp,则必然获取到的name是null
			if(name!=null){
				out.print(name);
				
				
				System.out.println();
		%>
			<a href="invalidate.jsp">注销</a>
		<%
				
			}else{//如果没有登录,应该跳转登录页面
				response.sendRedirect("login.jsp");
			}
			
		
		%>
</body>
</html>

a.jsp

代码语言:javascript
复制
<%@ 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>
		<%

			out.print(session.getAttribute("uname"));
			Cookie[] cookies = request.getCookies();
			for(Cookie cookie:cookies){
				if(cookie.getName().equals("JSESSIONID")){
					System.out.print("JSESSIONID"+cookie.getValue());
				}
			}
		
		
		%>
</body>
</html>

通过登录和直接访问a.jsp在控制台分别输出

cookie和session的区别:

session

cookie

保存的位置

服务端

客户端

安全性

较安全

较不安全

保存的内容

Object

String

登录后点击 注销 跳回到登录页面

再次访问a.jsp时

登录之后不点击注销,直接访问a.jsp页面

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档