I have a GAE CloudEndpoints project and I am having trouble using Java Sessions to maintain session state. Here is what I have done so far:
First I enabled Sessions in the appengine-web.xml file.
I then created a "login object" to hold the useremail and password. I also made it Serializable (as I read this was a requirement?)
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Session implements Serializable{
private static final long serialVersionUID = 4013168535563327001L;
@Persistent
String email;
@Persistent
String password;
public String getemail() {
return email;
}
public void setemail(String email) {
this.email = email;
}
public String getpassword() {
return password;
}
public void setpassword(String password) {
this.password = password;
}
}
Then I created a controller to create the session and store the login object as an attribute inside of it:
@ApiMethod(name="setLoginTest")
public Object setloginTest(HttpServletRequest request, User myLogin){
//Just setting some values into myLogin
//for production, this will compare submitted values against a DB.
myLogin.email = "Jason";
myLogin.password = "123";
//Creating the session object
HttpSession mySession = request.getSession(true);
//Trying to set an attribute that holds the login object
//(i.e. i want to store the username in the session)
mySession.setAttribute("loginObj", myLogin);
return myLogin;
}
If I call this endpoint, the system returns the myLogin object no problem and I know the mylogin object has the username "jason" and the pw "123"
So, the final step if for me to create the endpoint that checks to see if the session has some user data or null. ideally, I would take some action if logged in, and if not logged in, return a "not logged in" message.
@ApiMethod(name="getSessionTest")
public Object getSessionTest(HttpServletRequest request, User myLogin){
//getting the current session info
HttpSession mySession = request.getSession(false);
//Always returns NULL which is not right!!
return mySession.getAttribute("loginObj");
}
however, the trouble is that the "Getsessiontest" endpoint always returns null. Please help!