0

i work on a little project and i need to do a 'login' rediriction but without database just a test on a username/password strings.

I did a index.jsp file where i have a form with input text field for login and same for the password.The method is post:

<form name="form" method="post" action="http://localhost:8080/Miniproject/Authservlet">
   <label for="login">Login</label><br>
  <input type="text" name="login" id="login">
  <br>
  <label for="password">Password</label><br>
  <input type="password" name="password" id="password">
  <br><br>
  <input type="submit" value="Me connecter" id="button_submit">
</form> 

then i have a servlet with a dopost method defined like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    boolean result = Authentication.checklogin(login,password);
    request.setAttribute("result",result);
    request.setAttribute("login",login);
    request.setAttribute("password",password);
    if (result) {
        RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/view/welcome.jsp");
        dispatcher.include(request, response);
    } else {
        RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/view/error.jsp");
        dispatcher.include(request, response);
    }


    doGet(request, response);
}

I have a Authentication class defined like this (I removed setters and getters for my post but they exist in ther class) :

public class Authentication {

    public String login;
    public String password; 

    public static boolean checklogin( String login ,String password ) {
        if (login == "username" && password=="password") {
            return true;
        } else {
            return false;
        }
    }

}

the form is submitted, the login/password string that i get from "request parameters" are ok, the method is static and i return true or false but when i do this i always have a result = null in the servlet but i should get a result = true or false... i don't understand why

i tried to do it with int, string but i have no return.the Authentication class is imported in the servlet like this:

import package.model.Authentication;

if someone can explain me why the method is not working it would be perfect, thanks :D

Turing85
  • 18,217
  • 7
  • 33
  • 58
Vaaroz
  • 13
  • 6
  • i already seen this topic, the '==' comparator should work with no problem for a string comparaison :/ – Vaaroz Apr 18 '18 at 14:53
  • Try `"username".equals(username) && "password".equals(password)` instead. – dgg Apr 18 '18 at 14:55
  • i'm trying this right now – Vaaroz Apr 18 '18 at 14:57
  • the .equals() worked, i maybe misunderstood what they said in the other topic but i thought the '==' was ok and will work but seems i was wrong, thanks you all :) – Vaaroz Apr 18 '18 at 14:59
  • "*i already seen this topic, the '==' comparator on should work with no problem for a string comparaison :/*" - The behaviour of `==` reference-types is clearly specified in [JLS §15.21.3](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21.3). Changing its behaviour for `String` and `String` only would be a bizarre corner case. – Turing85 Apr 18 '18 at 15:01

0 Answers0