How to get all Session values and names?

喜你入骨 提交于 2021-02-07 08:00:25

问题


if i have values in a session and i need to get all the values in a session like

String[] name = request.getParameterValues("values");
HttpSession session = request.getSession();

for(String temp:name)
{
    if(temp.equalsIgnoreCase("a"))
    {
        session.setAttribute("a", temp);
        out.println("a is Running<br>");
    }

    if(temp.equalsIgnoreCase("b"))
    {
        session.setAttribute("b", temp);
        out.println("b is Running<br>");
    }

    if(temp.equalsIgnoreCase("c"))
    {
        session.setAttribute("c", temp);
        out.println("c is Running<br>");
    }

    if(temp.equalsIgnoreCase("d"))
    {
        session.setAttribute("d", temp);
        out.println("d is Running<br>");
    }

    if(temp.equalsIgnoreCase("e"))
    {
        session.setAttribute("e", temp);
        out.println("e is Running<br>");
    }

    if(temp.equalsIgnoreCase("f"))
    {
        session.setAttribute("f", temp);
        out.println("f is Running<br>");
    }
}
  • if I get a set of checkbox values to a string. Im setting all the values which are selected in a .jsp to a session object. I need to retrieve only the selected values in a jsp which are saved in the above code.

回答1:


Enumeration<String> attributes = request.getSession().getAttributeNames();
while (attributes.hasMoreElements()) {
    String attribute = (String) attributes.nextElement();
    System.out.println(attribute+" : "+request.getSession().getAttribute(attribute));
}



回答2:


        Enumeration e = (Enumeration) (session.getAttributeNames());

        while ( e.hasMoreElements())
        {
            Object tring;
            if((tring = e.nextElement())!=null)
            {
                out.println(session.getValue((String) tring));
                out.println("<br/>");
            }

        }

this code is also working perfectly thanks for the spark reply @karim mohsen




回答3:


Enumeration keys = session.getAttributeNames();
while (keys.hasMoreElements()){
   String key = (String)keys.nextElement();
   out.println(key + ": " + session.getValue(key) + "<br>");
}

this code is working on jsp




回答4:


Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
    String temp= attributeNames.nextElement();
            if(temp.equalsIgnoreCase("a"))
            {
                session.setAttribute("a", temp);
                out.println("a is Running<br>");
            }
            if(temp.equalsIgnoreCase("b"))
            {
                session.setAttribute("b", temp);
                out.println("b is Running<br>");
            }
            if(temp.equalsIgnoreCase("c"))
            {
                session.setAttribute("c", temp);
                out.println("c is Running<br>");
            }
            if(temp.equalsIgnoreCase("d"))
            {
                session.setAttribute("d", temp);
                out.println("d is Running<br>");
            }
            if(temp.equalsIgnoreCase("e"))
            {
                session.setAttribute("e", temp);
                out.println("e is Running<br>");
            }
            if(temp.equalsIgnoreCase("f"))
            {
                session.setAttribute("f", temp);
                out.println("f is Running<br>");
            }
}



回答5:


This Code is done in JSP

emun= session.getAttributeNames(); // getting all attributes names in session %>

来源:https://stackoverflow.com/questions/33368963/how-to-get-all-session-values-and-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!