Home>

We are creating a calendar that can manage the schedule.
When you enter an appointment in the appointment input form on the calendar jsp screen, through the processing servlet
I am creating a system that the schedule is reflected on the calendar jsp screen.

Currently the first input is reflected, but when you send the second time
There is a problem that the first appointment is deleted.

### Applicable source code
            if (session.getAttribute ("map") == null) {
                HashMap<Integer, String>map = new HashMap<Integer, String>();
                map.put (tb.getDay (), tb.getTask ());
                session.setAttribute ("map", map);
                getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
            } else {
                map.put (tb.getDay (), tb.getTask ());
                session.setAttribute ("map", map);
                getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);


Source code

      // The part to write the schedule on the calendar
        else if (a.equals ("2")) {
            // bean declaration
            TaskBean tb = new TaskBean ();
            String task = req.getParameter ("task");// Scheduled
            tb.setTask (task);
            String d = req.getParameter ("day");// day
            int day = Integer.parseInt (d);
            tb.setDay (day);
            // Declaration of map
            HashMap<Integer, String>map = new HashMap<Integer, String>();
            HttpSession session = req.getSession ();
            if (session.getAttribute ("map") == null) {
                map.put (tb.getDay (), tb.getTask ());
                session.setAttribute ("yotei", tb);
                session.setAttribute ("map", map);
                getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
            } else {
                map.put (tb.getDay (), tb.getTask ());
                session.setAttribute ("yotei", tb);
                session.setAttribute ("map", map);
                getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
            }
        }


`` ```

What I tried

When the screen is input the second time, the same process is passed, so I wondered if a new map will be created and the retention will disappear
I tried to put hashmap in the if statement, but I couldn't get the value in else {}.
I don't know what to do.

When passing the second time, I would like to put the acquired contents on the existing map and make a screen transition.
What should I do…

Supplementary information (FW/tool version, etc.)

Eclipse

  • Answer # 1

    I think that the posted code will pass through the hashmap and the previous data will be lost.

    That's right.
    I'm updating a new HashMap every time, so if I refer to it, the existing information will be lost.

    So, if you have a HashMap once created, use it.

              // Declaration of map
                HashMap<Integer, String>map = new HashMap<Integer, String>();
                HttpSession session = req.getSession ();
                if (session.getAttribute ("map") == null) {
    //abridgement
                } else {
                    map = (HashMap<Integer, String>) session.getAttribute ("map");
                    map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
                }

    I think it's more natural to get a HashMap from the beginning and create a new one if it's Null.

    HttpSession session = req.getSession ();
    HashMap<Integer, String>map = (HashMap<Integer, String>) session.getAttribute ("map")
    if (map == null) {
        map = new HashMap<Integer, String>();
    }

  • Answer # 2

    First. .. In the source you provided

    Declaration of // map
                HashMap<Integer, String>map = new HashMap<Integer, String>();

    You initialize the map every time.
    so. ..

              if (session.getAttribute ("map") == null) {
                    map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
                } else {
                    map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
                }


    Both if and else

                  map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);


    Because, at that time, only the input from the screen is stored in the map. (Cause)

    I have to retrieve the map from the session

              if (session.getAttribute ("map") == null) {
                    map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
                } else {
                    map = (Map) session.getAttribute ("map");// Add
                    map.put (tb.getDay (), tb.getTask ());
                    session.setAttribute ("yotei", tb);
                    session.setAttribute ("map", map);
                    getServletContext (). getRequestDispatcher ("/ calendar.jsp"). forward (req, res);
                }

    How about this?

Related articles