问答详情
源自:4-2 session对象

session.getvalueanames()顺序不对

session_page1.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'session_page1.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>
 
  <body>
    <h1>&nbsp;session内置对象</h1>
    <hr>
    <%
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
     Date d=new Date(session.getCreationTime());
     session.setAttribute("最喜欢的人是","最喜欢的人是");
     session.setAttribute("myz", "myz");
     session.setAttribute("是你的什么?", "是我的心肝儿");
     session.setAttribute("是我的心肝儿", "是我的心肝儿");
  session.setAttribute("是我的甜蜜饯儿", "是我的甜蜜饯儿");
  //session.setMaxInactiveInterval(10);
     %>
     session创建时间:<%=sdf.format(d)%><br>
     session的ID号:<%=session.getId() %><br>
     从session中获取用户名:<%=session.getAttribute("myz")%><br>
     <a href="session_page2.jsp" target="_blank">要上天啊</a>
    
  </body>
</html>

session_page2.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'session_page2.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>
 
  <body>
    session的ID号:<%=session.getId() %><br>
    从session中获取用户名:<%=session.getAttribute("myz")%><br>
    session保存的属性:<%
        String[] names=session.getValueNames();
        for(int i=0;i<names.length;i++){
         out.println(names[i]+"&nbsp;&nbsp;");
        }
     %>
  </body>
</html>


提问者:ERPPASSPASS 2017-03-03 20:00

个回答

  • 看着黎明庆幸
    2017-03-13 12:43:16

    同问~

  • Zhong1
    2017-03-09 10:24:00

    这个可能因为存储的时候是已键值对的形式 存储就像 Map的<key,value>key是没有顺序的

  • 澈水丶
    2017-03-04 13:12:44

    同问.貌似我用getvalueanames()他先返回的是names[1],最后返回的是names[0]