Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java/JSP Problem

Options
  • 08-02-2005 4:43pm
    #1
    Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭


    I'm having a problem getting a value from a .jsp page while using Java. Hopefully I can word this right.

    I have a jsp page which has a Select box who's contents are filled in by a for loop:
    <SELECT NAME="guitars" id="guitars">
    <%
    ArrayList guitars = (ArrayList) request.getAttribute("guitars") ;
    for (int i = 0 ; i < guitars.size() ; i++)
    {
       Guitar g = (Guitar) guitars.get(i) ;
    %>
    <option Value="<%= g.getGuitarId() %>"> <%= g.getManName() %> <%= g.getGuitarModel() %>
    <%
    }    
    %>
    </SELECT>
    

    Now I want to be able to find out what the value is of the Select box when I hit Submit. I was trying to use something like:
    String guitar_wanted = (String) request.getAttribute("guitar.guitars.selected");
    

    But I don't know what to make the "guitar.guitars.selected" bit. Can anyone help?


Comments

  • Closed Accounts Posts: 324 ✭✭madramor


    Name=X in html

    String sx = request.getParameter("X"); in jsp/servlet


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Hey, check out the JSTL taglibs too. Cuts out all the messy scriptlets (<%= and %> and the like)
    For example your code could be written like this:
    <%
    pageContext.setAttribute("allGuitars", arrayListOfGuitars);
    %>
    <SELECT NAME="guitars" id="guitars">
    
    <c:forEach var="guitar" items="allGuitars">
    
    <option Value="<c:out value="${guitar.guitarId}"/>"> <c:out value="${guitar.manName}"/> <c:out value="${guitar.guitarModel}"/> </option>
       </c:forEach>
    </SELECT>
    

    But ya, the select passes the VALUE from the selected option to the next page. People always get stuck on that one because often the VALUE and the LABEL (Manufacturer - Model in this case) are different.


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    madramor wrote:
    Name=X in html

    String sx = request.getParameter("X"); in jsp/servlet

    I tried that already and it was just returning a null.

    Enygma, I'll leave the JSTL things for now. What should I have in my code to get the value from the Select?


  • Closed Accounts Posts: 324 ✭✭madramor


    feylya wrote:
    I tried that already and it was just returning a null.

    Enygma, I'll leave the JSTL things for now. What should I have in my code to get the value from the Select?

    thats how its done, so your causing the problem

    always try using a plain html and java page first,
    until you know how to do it

    html->containing form->containing select->goto jssp/servlet

    jsp/servlet->extract the select value


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    If you're getting null then check the source code of the first page through your browser. Make sure there are values for each option in the select.

    The name of the select is "guitar" so to get that value on the next page you'd do this:

    String guitar = request.getParameter("guitar");

    Try to simplify all your code to where it's so simple that it just works, then build up from there.


  • Advertisement
Advertisement