Search code examples
javagoogle-app-enginesessionservletssuperclass

Java superclass user for logging in


I'm having some problems with my superclass User that contains the username and password of a student, teacher and company.

Everything works fine, but when I log in with a certain username/password combination the data gets lost. I still have the data from student/teacher/company but no longer the User data.

User.java (gebruiker in dutch, this is the superclass):

public class Gebruiker {
private String gebruikersnaam, wachtwoord;

public Gebruiker(String gn, String ww) {
    gebruikersnaam = gn; //username
    wachtwoord = ww; //password
}

public Gebruiker() { 
    //Default constructor
}

    //Getters and setters...
}
}

Student.java (leerling in dutch):

public class Leerling extends Gebruiker implements Serializable {
private static final long serialVersionUID = -3586223028920551117L;
private String naam, email, leerlingnr;

public Leerling(String gn, String ww, String nm, String em, String ln) {
    super(gn, ww);
    naam = nm;
    email = em;
    leerlingnr = ln;
}

    //Getters and setters...
}

Up until this point the code works fine. I can create a student with a username and password like this:

Gebruiker g1 = (Gebruiker) new Leerling("username", "password", "name", "email", "nr");

The problem comes when I login into my application and set the session with my user:

req.getSession().setAttribute("gebruikerObject", g1);

The moment I refresh the page. The username and password from the user object are null, 'name' 'email' and 'nr' stay normal. When I print gebruikerObject on my JSP page, I get an different return value every refresh:

com.appspot.AccentNijkerk.model.Leerling@16ee9e2
com.appspot.AccentNijkerk.model.Leerling@1c6b2e3
com.appspot.AccentNijkerk.model.Leerling@1fa486

I really hope you guys can help me out, thanks in advance.

PS: Im using Google App Engine, sessions are enabled


Solution

  • The class Gebruiker should be serializable as well.

    Also, you should learn about the this keyword, and use it to have good, readable, understandable variable names, respecting the Java naming conventions:

    public Gebruiker(String gebruikersNaam, String wachtWoord) {
        this.gebruikersNaam = gebruikersNaam;
        this.wachtWoord = wachtWoord;
    }