<%@page import="java.sql.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ 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>JSP内置对象-session</title> </head> <body> <h1>session常用方法</h1> <hr> <% SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date d = new Date(session.getCreationTime()); session.setAttribute("userName", "张三"); session.setAttribute("password", "123456"); session.setAttribute("age", "20"); //设置当前session最大生成周期单位是秒 session.setMaxInactiveInterval(10);//10秒钟 %> Session创建时间:<%=sdf.format(d)%><br> Session的ID编号:<%=session.getId()%><br> 从session中获取用户名:<%=session.getAttribute("userName")%><br> <a href="neizhiSession2.jsp" target="_blank">跳转到neizhiSession2</a> </body> </html>
<%@page import="java.sql.Date"%> <%@page import="java.text.SimpleDateFormat"%> <%@ 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>JSP内置对象-session</title> </head> <body> <h1>session常用方法</h1> <hr> <% %> Session的ID编号:<%=session.getId()%><br> 从session中获取用户名:<%=session.getAttribute("userName")%><br> Session中保存的属性有:<% String[] names = session.getValueNames(); for (int i = 0; i < names.length; i++) { out.println(names[i] + " "); } %><br> </body> </html>