Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:13:44+00:00 2026-06-15T10:13:44+00:00

This is for my java webapps class in college. A user can add add

  • 0

This is for my java webapps class in college.
A user can add add attributes to a session as a pair (name and value). So I use a hashmap.
The user has the possibility to add such pairs multiple times in the same session. Therefore I want to store the entire hashmap in the session and each time the submit button is pressed the pair should be added to the Map. However with this code only the last added pair is shown. I have no idea why this happens

 Map<String, String> lijst;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();
    if (session.isNew()) {
        lijst = new HashMap<String, String>();
        session.setAttribute("lijst", lijst);
    } else {
        lijst = (HashMap<String, String>) session.getAttribute("lijst");
    }

    String naam = request.getParameter("naam");
    String waarde = request.getParameter("waarde");


    lijst.put(naam, waarde);
    printResultaat(request, response);

}

private void printResultaat(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sessie demo</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Sessie demo</h1>");
        out.println("<a href=\"voegtoe.html\">Toevoegen</a>");
        HttpSession session = request.getSession();
        out.println("<h3>Sessie gegevens</h3>");
        out.println("<p>Sessie aangemaakt op: " + new Date(session.getCreationTime()) + "</p>");
        out.println("<p>Sessie timeout: " + ((session.getMaxInactiveInterval()) / 60) + "</p>");
        HashMap<String, String> lijstAttr = (HashMap<String, String>) session.getAttribute("lijst");
        Iterator it = lijstAttr.entrySet().iterator();
        out.println("<h3>Sessie attributen (naam - waarde)</h3>");
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            out.println(pairs.getKey() + " " + pairs.getValue());
            it.remove();
        }
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:13:45+00:00Added an answer on June 15, 2026 at 10:13 am

    Looks like you are explicitly cleaning out the hashmap every time you print it.

        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            out.println(pairs.getKey() + " " + pairs.getValue());
            it.remove();   // <-----------
        }
    

    Just remove the line with it.remove()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use to deploy tomcat webapps through eclipse but I have this error: WARNING:
When I write Java webapps, I usually use JSTL tags. I think that these
I have tried execute this class as a java application and have got this
Answer: My Spring 3 Java webapp is acme. I had this for my JQuery
This Java code compiles fine, but when I try to run it I get:
In this Java desktop GUI, the web services communication layer is in Java, the
For this Java code: stmt.addBatch( INSERT INTO Bills (BillDateTime, Table, Item, NoAttended, Service, Payment,
i have this java code: ArrayList<MessageMap> ids = getNewMail(UserID, type, maxIdMessage); Message[] message =
I'm using this java script library to show ratings http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I'm using below
I have this Java code, and I want to do the same thing in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.