Comparison request.getParameter with JSP string

Asked

Viewed 452 times

0

I’m wanting to pick up what the user will type with request.getParameter and compare with a String and if the value the user enters is equal to string, the user is redirected to the page but this is not happening.

 String login = request.getParameter("login");
      String senha = request.getParameter("senha");

     if(login=="Felipe_Massa_10" && senha=="felipemassa1010"){
      RequestDispatcher rd=request.getRequestDispatcher("/CadastroProdutos.jsp");
     }

2 answers

0

Never use == to compare string in Java. The == is used to compare references in memory, equating the . equals() is used to compare the values themselves, but remember that possible nullPointerExceptions may occur, so put the constant on the left side, using a practice called "Never-null-Pointer", and it would look like this: "Felipe_massa_10". equals(login) instead of login.equals("Felipe_massa_10"). If login is null, then false is returned.

0


See the question how to compare strings in Java?

As you are doing, the comparison is being made by the reference and not by the contents of the string. Use equals() to perform the verification:

String login = request.getParameter("login"),
       senha = request.getParameter("senha");

if(login.equals("Felipe_Massa_10") && senha.equals("felipemassa1010"))
   request.getRequestDispatcher("/CadastroProdutos.jsp")
          .forward(request, response);

Browser other questions tagged

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