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

2022-11-03 14:52:45 浏览数 (1)

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

0 人点赞