Search code examples
javastringauthenticationvaadin7

Java: Vaadin Login-screen Web-App behaves unnaturaly


I am currently building a java server-side WebApp using Vaadin. Now, I am a Newb regarding Vaadin, so please be gentle. ;)

Here's my issue: I am currently building the Login screen. I set up a user-name and password as global variables for test purposes. On the login screen, a user just enters his user-name and password, and upon the login button the code compares the values entered in the text fields with those of the global variables. If the values match, a notification pops-up stating the login was successful, otherwise you just get an error notification.

Here's the problem: when I call the String value of the username.getValue() (from the textfield) and compare it to the username stored in the global variable, the if-else statement always jumps to the else branch which indicates that, for some esoteric reason, the Strings don't match. Thing is, they do. Just to make sure I didn't oversee anything I'm printing the values entered in the text fields out on the console, and the way .getValue() fetches them corresponds exactly to the way the data is stored in the global variables.

Does anyone have any idea why this piece of code tries to screw with my sanity?

Here's the code:

package com.example.vaadintestgui;

import java.util.Locale;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("vaadintestgui")
public class VaadintestguiUI extends UI {

private static final String username = "admin";
private static final String password = "r00t";

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = VaadintestguiUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

    UI.getCurrent().setLocale(new Locale("en"));
    Page.getCurrent().setTitle("Login");



    final TextField uname = new TextField("Username:");
    final PasswordField pass = new PasswordField("Password:");


    Button loginButton = new Button("Login");
    loginButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            Notification.show("Cheking Credentials...");

            // Just for debugg purposes:
            System.out.println("uname: "+uname.getValue()+" password: "+pass.getValue());

            // Something is wrong here....:
            if(uname.getValue() == username && pass.getValue() == password){
                Notification.show("Login Succesfull!");
            } else {
                Notification.show("Wrong login credentials. Please try again.");
                }
        }
    });

    layout.addComponent(uname);
    layout.setComponentAlignment(uname, Alignment.MIDDLE_CENTER);
    layout.addComponent(pass);
    layout.setComponentAlignment(pass, Alignment.MIDDLE_CENTER);
    layout.addComponent(loginButton);
    layout.setComponentAlignment(loginButton, Alignment.MIDDLE_CENTER);
}

}

I am running the app on a client-side tomcat8 localhost server (for now). Any help would be greatly appreciated!


Solution

  • Comparing strings should always be done with .equals() instead of ==