前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jsp之session学习:session.setAttribute(String name,Object value)&session.getAttribute(String name);

jsp之session学习:session.setAttribute(String name,Object value)&session.getAttribute(String name);

作者头像
全栈程序员站长
发布2022-11-03 14:52:45
4980
发布2022-11-03 14:52:45
举报
文章被收录于专栏:全栈程序员必看

jsp之session学习:session.setAttribute(String name,Object value)&session.getAttribute(String name);

part_1 : index.jsp

代码语言:javascript
复制
<%@ page language="java" import="java.util.*" 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%>">
	</head>

	<body>
		Welcome page

		<form action="servlet/BServlet_1206_SetRequestEncoding" method="post">
			username :
			<input type="text" name="username" value="click2Input" />
			<br>
			password :
			<input type="password" name="password" />
			<br />
			<input type="submit" value="SubmitTest" />
		</form>
		<br>
		<hr />
		<a href="servlet/BServlet_1206_SetRequestEncoding">Get test</a>
		<br />
		<a href="/ServletDemoProject/jsps/table-for-loop.jsp">点击显示循环表格</a>
		<br />
		<a href="/ServletDemoProject/generate-sum/form-two-operators.jsp">点击输入两个数字并求和</a>
		<br />
		<a href="/ServletDemoProject/COOKIE-DEMO/set-cookie.jsp">点击演示Cookie</a>
		<br/>
		<a href = "/ServletDemoProject/SESSION-DEMO/A-session-set-attribute.jsp">点击设置session</a>
	</body>
</html>

part_2 : A-session-set-attribute.jsp

代码语言:javascript
复制
<%@ page language="java" 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>My JSP 'A-session-set-get-attribute.jsp' starting page</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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<%--Declaration --%>

	<%--Script --%>
	<%
		java.text.SimpleDateFormat sdf_date = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		java.text.SimpleDateFormat sdf_moment = new java.text.SimpleDateFormat(
				"HH:mm:ss a");
		String date_of_today = sdf_date.format(new java.util.Date());
		String moment_of_now = sdf_moment.format(new java.util.Date());
		session.setAttribute("date_of_today", date_of_today);
		session.setAttribute("moment_of_now", moment_of_now);
	%>


	<body>
		<h3>
			Set Attribute :
		</h3>
		<br />

		<h3>
			date_of_today :
			<%=date_of_today%>
			<br />
			moment_of_now :
			<%=moment_of_now%>
		</h3>
		<br />

		<a href="/ServletDemoProject/SESSION-DEMO/B-session-get-attribute.jsp">点击查看设置Session
			Attribute是否成功</a>
		<br />

	</body>
</html>

part_3 : B-session-get-attribute.jsp

代码语言:javascript
复制
<%@ page language="java" import="java.util.*" 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>My JSP 'B-session-set-get-attribute.jsp' starting page</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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<%
		String date_of_today = (String) session
				.getAttribute("date_of_today");
		String moment_of_now = (String) session
				.getAttribute("moment_of_now");
	%>

	<body>

		<h3>
			Get Attribute :
		</h3>
		<br />

		<h3>
			date_of_today :
			<%=date_of_today%>
			<br />
			moment_of_now :
			<%=moment_of_now%>
			<br />
		</h3>
		<br />

	</body>
</html>

Time : 2016-12-08-%-23:50

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181107.html原文链接:https://javaforall.cn

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • jsp之session学习:session.setAttribute(String name,Object value)&session.getAttribute(String name);
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档