So I have 2 variables. One is written in javascript, the other in a scriplet in jsp. They both are strings and they both have a value of "4" when I print them. When I do .getClass() they're both of the string class but when I print out asking if they are .equals() OR even if I do (which I know is incorrect for strings) == it prints out false.

These are my variables and how they are declared.

String showingValue = "<script>document.writeln(selectedCopyValue)</script>";

String val4 = "4";

The selectedCopyValue is a varible that is written in javascript above. It is declared (for now as it is hardcoded for testing) as:

String selectedCopyValue = "4";

So why when I do something even as extensive as:

out.print(val4.trim().toString().equals(showingValue.trim().toString()));

Does it print out false?

有帮助吗?

解决方案

When you print showingValue, the browser executes the script and displays 4. When you compare the strings, it doesn't execute the script, so it literally compares the strings:

"4" is not equal to "<script>document.writeln(selectedCopyValue)</script>"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top