0

I am trying to call a HashMap value in jsp page but got error there and error says

There was an unexpected error (type=Internal Server Error, status=500). For input string: "res"

My code;

<c:forEach items="${onlineExamList}" var="item"
                                        varStatus="loop">

                                        <div>
                                            <b>${item.question1}</b><br>

                                            <div class="radio">
                                                <label><input type="radio" value="a"
                                                    name="answers[${loop.index}]">${item.option1}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="b"
                                                    name="answers[${loop.index}]">${item.option2}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="c"
                                                    name="answers[${loop.index}]">${item.option3}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="d"
                                                    name="answers[${loop.index}]">${item.option4}</label>
                                            </div>
                                            <input type="text" name="rightAnswer"
                                                value="${item.rightAnswer}">



//problem in this line
                                            <c:if test="${result != null}">
                                                <br>
                                                <br>
                                                <b>Your answer: ${result.get("res"+loop.index).get(1)}</b>

                                                <br>
                                            </c:if>
                                        </div>

                                        <hr />
                                    </c:forEach>

And this is how i have set hashmap from controller

Map<String, List<String>> mapResult = new HashMap<String, List<String>>();

        int totalScore = 0;

        for (int i = 0; i < answers.answers.size(); i++) {
            List<String> result = new ArrayList<>();
            String res = "Wrong";
            if (answers.answers.get(i).equals(answers.rightAnswer.get(i))) {
                res = "Correct";
                totalScore+=10;
            }

            result.add(res);
            result.add(answers.answers.get(i));
            result.add(answers.rightAnswer.get(i));

            mapResult.put("res" + i, result);
        }

        ra.addFlashAttribute("result", mapResult);
        ra.addFlashAttribute("score", totalScore);

Same things printed on java page

for (int i = 0; i < 10; i++) {
            System.out.println(mapResult.get("res" + i).get(0));
            System.out.println(mapResult.get("res" + i).get(1));
            System.out.println(mapResult.get("res" + i).get(2));
            System.out.println("...................................");;
        }

How can I print hashmap value in jsp page?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nishan Dhungana
  • 831
  • 3
  • 11
  • 30
  • Possible duplicate of [How to loop through a HashMap in JSP?](https://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp) – Alan Hay Dec 18 '18 at 09:45
  • Basic rule is, if you see a 500 error, issue is with your server code, no issue with JSP code. post the stack trace as well. – raviraja Dec 19 '18 at 07:00

3 Answers3

0

Please check try this.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${currentLoggedInUsersMap}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
0

The mapResult object is a map containing string arrays
Hashmap<String, List<String>>
you may need to first get the Array from the map object and then iterate over it.
this may help?

<table> <c:forEach var="results" items="${resultsMap['res' + loop.index]}" varStatus="loop"> <tr> <c:forEach var="answer" items="${results}" varStatus="status"> <td> <td>$answer</td> </td> </c:forEach> </tr>
</c:forEach>
</table>

0

I solve the problem by concating the string using concat like this

${result.get("res".concat(loop.index)).get(1)}
Nishan Dhungana
  • 831
  • 3
  • 11
  • 30