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

来源:互联网 发布:vb数值转字符串 编辑:程序博客网 时间:2024/05/01 22:47

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

part_1 : index.jsp

<%@ 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
<%@ 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">点击查看设置SessionAttribute是否成功</a><br /></body></html>

part_3 : B-session-get-attribute.jsp


<%@ 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

0 0