0

I have an object (collection) that I am putting into request before constructing a JSP page based on the state of the object.

        Map<Integer, QuestionBO> questionsIdsMap = new TreeMap<Integer, QuestionBO>();

        for (QuestionBO question : questionsForSubject) {
            questionsIdsMap.put(question.getQuestionId(), question);                
        } 

        request.setAttribute("questionsForSubject", questionsIdsMap);

Then I do some manipulations with the form and submit the whole page back to the servlet for processing.

    <jsp:useBean id="questionsForSubject" class="java.util.Map" scope="request"/>

    <c:if test="${not empty questionsForSubject}">
    <form  action="/TutorWebApp/controller" method="POST" name="addQuestionForm">
        <input type="hidden" name="command" value="add_question_list" />
        <input type="hidden" name="testName" value="${testName}"/> 
        <input type="hidden" name="questionsForSubject" value="${questionsForSubject}"/>
        <table border ="1">
            <tbody>
                <c:forEach items="${questionsForSubject.keySet()}" var="questionID">
                    <tr>
                        <td>
                            <input type="checkbox" name ="choosen_question" value="${questionID}">
                            ${questionsForSubject.get(questionID).getQuestion()}
                            <br />
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
        <input type="submit" value="Add questions"/>              
    </form> 
    </c:if>  

What is the conventional way of passing of the previously mentioned collection to make it available on the servlet-side?

I should do it without any frameworks.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
Ray
  • 1,788
  • 7
  • 55
  • 92

2 Answers2

0

Since you have used

request.setAttribute("questionsForSubject", questionsIdsMap);

you don't need to use the jsp:useBean tag.
For the display, try

<c:forEach items="${questionsForSubject}" var="current">
                <tr>
                    <td>
                        <input type="checkbox" name ="choosen_question" value="${current.key}">
                        ${current.value}
                        <br />
                    </td>
                </tr>
</c:forEach>
rickz
  • 4,324
  • 2
  • 19
  • 30
0

The request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the JSP will not be available when you call servlet.

Check detailed answer: Passing an object from JSP page back to Servlet

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • I saw this post. I tried to put value on jsp as code request.getSession().setAttribute("questionsForSubject") code and get this value on servlet as code request.getSession().getAttribute("questionsForSubject") code. But value object in thic case equals NULL. – Ray May 02 '12 at 08:49