10

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.
dguay
  • 1,635
  • 15
  • 24
Manoj Krishna
  • 347
  • 1
  • 4
  • 12

6 Answers6

20
Enumeration<String> attributes = request.getSession().getAttributeNames();
while (attributes.hasMoreElements()) {
    String attribute = (String) attributes.nextElement();
    System.out.println(attribute+" : "+request.getSession().getAttribute(attribute));
}
Jonatan H.
  • 57
  • 1
  • 1
  • 9
Max CodeSmith
  • 467
  • 3
  • 13
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

Manoj Krishna
  • 347
  • 1
  • 4
  • 12
1
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

Arpico ICT
  • 121
  • 1
  • 5
  • getValue and putValue are deprecated. New method names are getAttribute and setAttribute. – D.A.H May 14 '22 at 09:56
0
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>");
            }
}
karim mohsen
  • 2,164
  • 1
  • 15
  • 19
0

This Code is done in JSP

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

One-liner:

By converting Enumeration to a list or stream (Improved upon Max CodeSmith's answer)

HttpSession session = request.getSession();

Collections.list(session.getAttributeNames()).forEach(name -> System.out.println(name + " : " + session.getAttribute(name)));

More info:

  1. Iterate an Enumeration in Java 8

  2. How do I turn a Java Enumeration into a Stream?

Enfield Li
  • 1,952
  • 1
  • 8
  • 23