Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-17-2012, 06:23 PM   PM User | #1
KKLU
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
KKLU is an unknown quantity at this point
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");
            }
         %>
KKLU is offline   Reply With Quote
Old 05-17-2012, 06:37 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 05-17-2012, 07:29 PM   PM User | #3
KKLU
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
KKLU is an unknown quantity at this point
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?
KKLU is offline   Reply With Quote
Old 05-17-2012, 07:49 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
checkbox, jsp

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:22 PM.


Advertisement
Log in to turn off these ads.