Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JSP and checkbox values

    Hello. Need some help. From the code below, JSP should print "True" only if the combination of A and C is submitted. All other false. It does not work though (even my teacher was lost ).

    Excuse me, I'm new to this.

    Thanks

    HTML
    Code:
    <form method="get" action="index.jsp">
    Question:<br>
    a) Answer A <input type="checkbox" name="question" value="a"><br>
    b) Answer B <input type="checkbox" name="question" value="b"><br>
    c) Answer C <input type="checkbox" name="question" value="c"><br>
    <input type="submit">
    </form>
    JSP
    Code:
    <%
                        if (request.getParameter("question").equals("a") & request.getParameter("question").equals("c")) {
                            out.print("True");
                        } else {
                            out.print("False");
                        }
    %>
    also tried

    Code:
    <%!            
    String[] question;
    %>
            <%
                question = request.getParameterValues("question");
                if (question[0] == "a" & question[1] == "c") {
                    out.print("True");
                } else {
                    out.print("False");
                }
             %>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its been too long since I've used JSP, but I'm quite sure ops are the same. & is a bit operator, not a logical operator, and in typical java it should throw a compilation error since you cannot cast from int to boolean. Change those from & to &&.
    Oh and use .equals. Strings are immutable, so while "a" == "a", "a" != new String("a"). Using .equals will always compare the value instead.

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. This works -- finally prints True.

    Code:
    <%!            
            String[] question;
            %>
            <%
                question = request.getParameterValues("question");
                if (question[0].equals("a") && question[1].equals("c")) {
                    out.print("True");
                } else {
                    out.print("False");
                
                }
              %>
    So, I guess the reason this does not work

    Code:
    if (request.getParameter("question").equals("a") && request.getParameter("question").equals("c")) {
                            out.print("True");
                        } else {
                            out.print("False");
                        }
    is because "question" has more than one value, hence needs to be added to a String and then compared?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sting should always be compared with equals or equalsIgnoreCase, otherwise it compares the object values themselves.

    For the retrieval, yeah that would be because its an array for checkbox, but I'm afraid I don't know exactly what's in it since JSP is different than how PHP handles checkboxes. I'm used to using JCheckBox within Java itself, but that is entirely different than how its handled from the request of JSP.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •