Comparison request.getParameter with String JSP

0

I want to get what the user will type with request.getParameter and compare it with a String and if the value that the user type is equal to the string, the user will be 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");
     }
    
asked by anonymous 05.10.2017 / 01:48

2 answers

0

See the question how to compare strings in Java?

In the way you are doing, the comparison is being made by the reference and not by the contents of the string. Use equals() to perform 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);
    
09.10.2017 / 03:43
0

Never use == to compare string in Java. == is used to compare benchmarks in memory, whereas .equals () is used to compare the values themselves, but remember that possible nullPointerExceptions can occur, then place the constant on the left using a practice called "never -null-pointer ", and it would look like:" Felipe_Massa_10 ".equals (login) instead of login.equals (" Felipe_Massa_10 "). If login is null, then false is returned.

    
19.02.2018 / 16:19