0

What I've been trying to do is to display radio buttons with a corresponding value inside a foreach loop. Then, I'd like to retrieve the value of the selected radio button on the next JSP page.

Here is part of my coding.

       <form action="EventList_to_seats_servlet" method="POST">     
            //There are irrelevant lines here.
            <c:otherwise>
                <c:forEach var="ID" items="${EventID}">
                //EventID, a list of Integer, comes from the previous servlet, 
                //set in HttpServletRequest
                //i.e. I wrote "request.setAttribute("EventID", EventID);" in the previous step.
                    <li>
                        <%= "Event ID: "%>   ${ID}
                        <input type="radio" name="${EventID}" value="ID" /><br><br>       
                    </li>
                 </c:forEach>
                 <input type="submit" value="Submit" name="submitOrderID" />
            </c:otherwise>      
        </form>

I'd like to retrieve the selected radio button's name and value on the next servlet.

Here is part of the next step (a servlet, called "EventList_to_seats_servlet").

BookingInfo bi = new BookingInfo();
//It's a bean dedicated to storing information entered by the client.

bi.setEventID(request.getParameter("ID"));
//It doesn't seem to be the right way to retrieve the selected radio button's name/value 

I've tested multiple ways to get the value from the JSP to the servlet, but I still can't find out how to do this, particularly because both the radio button's name and value are generated dynamically.

I'd appreciate if you would give any advice.

P.S.

I've found a similar posting here, but I honestly couldn't fully understand it, mainly because I'm not really sure how to use ${ }. ${ } seems to be used to access values set by HttpServletRequest (i.e. request.setAttribute("xxx", yyy);).

However, after I referred to this posting , I thought ${ } seemed to be used for getting values from a Bean too. In the above example, do you think that getting the yyy using ${xxx} is a common to use ${ } ?

Community
  • 1
  • 1
Hiroki
  • 3,893
  • 13
  • 51
  • 90
  • That looks pretty common to me, you'd need to look into [JSTL](http://stackoverflow.com/tags/jstl/info) for figuring out how that stuff works, or rather, [EL](http://www.tutorialspoint.com/jsp/jsp_expression_language.htm) in this context. – Zachary Craig Jan 06 '15 at 02:45
  • Thank you for your reply. I'm wondering if I could ask a silly question. According to [the second page](http://www.tutorialspoint.com/jsp/jsp_expression_language.htm), EL can be used to access "application data stored in JavaBeans components". However, what I'm accessing by my coding above is not a JavaBean, but an attribute which was set by "request.setAttribute("EventID", EventID);" Do you see anything I'm getting wrong? – Hiroki Jan 06 '15 at 08:12
  • No sir, i dont see anything, i belive ${} also looks in your request scope – Zachary Craig Jan 06 '15 at 08:14
  • I've been reading the links you showed and studying what I didn't know about JSTL & EL... So, if there is only a property's name inside `$( )`, this `$( )` is supposed to look at the property of [implicit objects](http://www.informit.com/articles/article.aspx?p=30946&seqNum=7), but if there is a property attached with its object name , will the `$( )` search the object's property? – Hiroki Jan 06 '15 at 10:25
  • It seems that I should study them by myself, anyways. – Hiroki Jan 06 '15 at 10:26
  • Yeah, im afraid youve mostly exhausted my trivial knowledge of EL and JSTL :) – Zachary Craig Jan 06 '15 at 10:27

1 Answers1

0

Change JSP and Servelet as below:

JSP:

<c:forEach var="ID" items="${EventID}">

                        <li>
                            <%= "Event ID: "%>   ${ID}
                            <input type="radio" name="selectedValue${id}" value="${ID.id}" /><br><br>       
                        </li>
 </c:forEach>

Servlet:

 String selected = request.getParameter("selectedProd" + getId());

OR
Just change JSP:

<c:forEach var="ID" items="${EventID}">

                    <li>
                        <%= "Event ID: "%>   ${ID}
                        <input type="radio" name="${EventID}" value="${ID.id}" /><br><br>       
                    </li>
</c:forEach>
Ye Win
  • 2,020
  • 14
  • 21
  • Thank you for your reply. It seems that the "ID" needs to be modified so that it has a property (i.e. ID.id), since the "EventID" is just a List of Integer and it doesn't have such a method. Could you please tell me how you think about it? – Hiroki Jan 06 '15 at 08:15
  • ID.id is loop auto generated id to know the loop index. – Ye Win Jan 06 '15 at 08:17
  • `request.getParameter("selectedProd" + EventID.getId())` When I tried the first one with my servlet having this line, this produced a compile time error. (There is no symbol for `getId()` at the servlet). At this time, I was prompted to import `com.sun.java.accessibility.util.EventID` at the servlet so there is some kind of progress anyway. – Hiroki Jan 06 '15 at 12:12
  • When I tried your second suggestion, it produced a run time error (It's http 500 error, saying that `javax.el.PropertyNotFoundException: The class 'java.lang.Integer' does not have the property 'id'.`) – Hiroki Jan 06 '15 at 12:13