Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all Session values and names?

Tags:

java

jsp

session

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.
like image 927
Manoj Krishna Avatar asked Oct 27 '15 13:10

Manoj Krishna


2 Answers

Enumeration<String> attributes = request.getSession().getAttributeNames();
while (attributes.hasMoreElements()) {
    String attribute = (String) attributes.nextElement();
    System.out.println(attribute+" : "+request.getSession().getAttribute(attribute));
}
like image 137
Max CodeSmith Avatar answered Nov 15 '22 19:11

Max CodeSmith


        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

like image 31
Manoj Krishna Avatar answered Nov 15 '22 18:11

Manoj Krishna