0

I have a Map in a servlet

Map<String, Boolean> seatsModelComplete = new LinkedHashMap<>();

The string represents seat number and the Boolean says if the seat is reserved or not.

I set the attribute in the request as follows:

request.setAttribute("seats", seatsModelComplete);

but I can't get it working in a JSP. Specifically, the map doesn't get iterated by the following foreach loop

<c:forEach items="${requestScope.seats}" var="seats">
...
</c:forEach>

Here is my Servlet:

@WebServlet(name = "SeatsServlet", urlPatterns = "/seats")

public class SeatsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<MovieDTO> movieDTOList = (List<MovieDTO>) request.getSession().getAttribute("movieDTOList");


int movie_id = Integer.parseInt(request.getParameter("movie_id"));

int hall_id = Integer.parseInt(request.getParameter("hall_id"));

int showtime_id = Integer.parseInt(request.getParameter("showtime_id"));

        MovieDTO selected_movie = null;

        Hall selected_hall = null;

        Showtime selected_showtime = null;
        for (MovieDTO m : movieDTOList) {
            if (m.getId() == movie_id) {
                selected_movie = m;
                break;
            }
        }
        if (selected_movie != null) {
            request.setAttribute("movie_title", selected_movie.getTitle());
        }

        for (Hall h : selected_movie.getHallList()) {
            if (h.getId() == hall_id) {
                selected_hall = h;
                break;
            }
        }
        if (selected_hall != null) {
            request.setAttribute("hall_id", selected_hall.getId());
        }

        for (Showtime s : selected_hall.getShowtimes()) {
            if (s.getId() == showtime_id) {
                selected_showtime = s;
                break;
            }
        }
        if (selected_showtime != null) {
            request.setAttribute("showtime_time", selected_showtime.getTime());
        }
        List<Seat> seats = selected_showtime.getSeats();


        int maxRows = (PropertyHolder.getInstance().getMaxRowsCount().toLowerCase().toCharArray()[0] - 'a') + 1;
        int maxNumbers = Integer.parseInt(PropertyHolder.getInstance().getMaxNumberCount());

        String aSeat;
        boolean isReserved;
        Map<String, Boolean> seatsModelComplete = new LinkedHashMap<>();
        for (int i = 0; i < maxRows; i++) {
            for (int j = 0; j < maxNumbers; j++) {
                aSeat = (((char)('A' + i)) + "").concat((j + 1) + "") ;
                isReserved = false;
                for (Seat seat : seats) {
                    if (seat.getRow().concat(seat.getNumber() + "").equals(aSeat)){
                        isReserved = true;
                    }
                }
                seatsModelComplete.put(aSeat, isReserved);
            }
        }
        request.setAttribute("seats", seatsModelComplete);
     // request.getSession().setAttribute("seats", seatsModelComplete); map gets passed only here, not in request scope

        request.getRequestDispatcher("/pages/seat_selection/seat_selection.jsp").forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
Roms
  • 25
  • 6
  • 1
    Possible duplicate of [How to loop through a HashMap in JSP?](https://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp) – Flown Aug 04 '17 at 07:13
  • are you sure that elements are added to this map? – Scary Wombat Aug 04 '17 at 07:16
  • figured out that, map passes in session but not in request scope – Roms Aug 04 '17 at 08:56
  • Problem is solved, I had iframe inside of m parent jsp page, so request scope could not reach out the iframe, so I just set up session scope for this attr – Roms Aug 05 '17 at 06:27

2 Answers2

0

Use this:

<c:forEach items="${seats}" var="seat">
     Key is ${seat.key}
     Value is ${seat.value}
</c:forEach>
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
sForSujit
  • 987
  • 1
  • 10
  • 24
  • Can you show your entire code of servlet in question?I think that will be better – sForSujit Aug 04 '17 at 08:26
  • @Roms Glad you get the solution – sForSujit Aug 04 '17 at 08:37
  • it was a problem that it is iframe , page in the page, and request scope could not reach iframe's jsp page, sorry I am just a student, new at this – Roms Aug 05 '17 at 06:25
  • @Roms That's okay, Learn from the mistakes, that's what student can progess – sForSujit Aug 05 '17 at 06:31
  • @Roms And as you said about – sForSujit Aug 05 '17 at 06:32
  • My first student project, so much going on here, and this part I just forgot about, sorry about that, for future will try to be more precise – Roms Aug 05 '17 at 06:36
  • Is there a better way to pass attr. to iframe window which is inside the parent one, then session scope? – Roms Aug 05 '17 at 06:41
0

Are you sure you initialized it? Stupid question but eh... Also,you could try something like

<c:forEach items="${seats}" var="seat">
  <option value="${seat.key}">${seat.taken}</option
</c:forEach>

Edit: it is possible that you messed something up with the jsp, you can always test if your collection is initialized properly and / or can be found within the proper scope:

<c:choose>
    <c:when test="${empty seats}">
        Seats are empty or null.
    </c:when>
    <c:otherwise>
        Seats are not empty or null
    </c:otherwise>
</c:choose>
Ognjen Mišić
  • 1,219
  • 17
  • 37