I don't know what the conditional branch value is.

Asked 2 years ago, Updated 2 years ago, 73 views

Prerequisites/What you want to achieve

I want to substitute the value received from the query string to the String variable and branch conditions.

Source Codes Affected

I'd like to display item 1…

Enter this URL.

http://localhost:8080/WebApplication1/programming_base1.jsp?param1=1&param2=2&param3=3

[programming_base1.jsp]

<%
    String product1 = request.getParameter("param1"); 
    String product2 = request.getParameter("param2"); 
    String product3 = request.getParameter("param3");
    
    out.print(product1);
    if(product1=="1"){←I can't get through here.
        out.println("Product1");
      out.print(product1);
    }
    if(product2=="2"){
        out.print("Product 2");
    }
%>

As a result, the browser displays 1…
Product 1 of item 1 is also not displayed, so it doesn't seem to meet the conditions of if...

Since product1 is a string type, I thought product1=="1" would pass, but it didn't.

What is the value obtained by the query string and substituted for product1?

Please let me know.

multi-post I also ask questions on the terratail

jsp

2022-09-30 19:11

1 Answers

Self-resolved.

It was resolved by using "1".equals(product1) instead of ==.

So I have to use equals for string type comparison.


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.