Java Strings Comparison Error

Asked

Viewed 174 times

1

I have the following situation.

I get a string that is in the application’s localStorage in a javascript variable.

<script>
    var sendForm = localStorage.getItem('f');
</script>

This same string I pass to a java variable to perform the proper procedures I will need.

<% 
    String id_formulario = "";
    id_formulario = "<script>document.write(sendForm)</script>"; 
%>

To perform tests I show this string with a

<%
    out.print(id_formulario);
%>

And that’s all right! it is the exact string that was in localStorage, but when I try to compare it with any string EQUAL java does not recognize that they are equal and simply ignore.

<%
    if(id_formulario.equals("QUALQUERVALOR")){
        out.print("É igual!!!");
    }
%>

Why is this happening? I’ve tried everything and I can’t solve this problem at all!!

  • I’ve tried equalsIgnoreCase, ==, ==, equals but all of this goes as if it’s not the same string I took from localStorage

  • By showing the length() of the received value and what I expected gave a difference that ended up visualizing the problem. The value I received had 41 characters while the one I expected had 22.

  • when you have the variable typed id_formulario alone, for example, he’s the way you hoped?

3 answers

0

Javascript variables stay in the front end while "<% code %>" codes are executed in the back end before sending the appropriate information.

One thing is you replicate data that is already by default added to javascript in the compilation of your back-end, another is data that is in the "localStorage" of your client’s browser.

After the request reaches your "client", the codes "<% %>" no longer exist, as they have already been processed by your server.

In this case, your server when compiling, did not have access to localStorage:

<script> var sendForm = localStorage.getItem('f'); </script>

Unable to correctly process the rest of the code that required this information.

  • Then he ended up storing in the id_formulario variable the literal value of "<script>Document.write(sendform)</script>", I just saw that the error was this, but approaching Kelvin, as I could receive the localStorage variable in a JSP variable?

  • @user63185 on the call of this "screen" so to say, you may require the client to send this data. That is, if you are opening a client screen, you need the ID, and so you will always send the ID on the HTTP call of this screen, be it POST or GET... So in your problem, as the server needs this data, you would pass this data also in the call of this screen, as would happen with a user ID.

  • I thought about it, the @Kelvin problem is that the operation is as follows, I have a form that the data is sent to a jsp file and this I’m talking about is another jsp file, which is a response page, so "would not have" how to send the id there understood? I am studying the possibility of working with cookies since java can directly manipulate them.

0

If what you said is correct.

but when I try to compare it with any string EQUAL java does not recognize that they are equal and simply ignore

So the tool you’re using is not compiling, or is trying to javascript thinking it’s like Java

0

Problem solved,

The assignment I was making from a javascript variable to a jsp was wrong, as java was writing it understood that the value of the serial variable literally "Document.write(sendform)". So never serial like something I’ve been waiting for.

Solution,

Java does not work with localStorage, but works with cookies, so I changed localStorage to cookies and now it works normally!

Thank you to everyone who responded, in the end it was just a survey that returned me a wrong answer and caused so much trouble!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.