I have a login screen on which i have a simple form with username and password fields. After successful login attempt I want to store user in the session and redirect to a home page. loginUser method is part of LoginResource class
@POST
public View loginUser(@FormParam("user") String username, @FormParam("pass") String password, @Context HttpServletRequest req)
{
LoginService ls = new LoginService();
User user = ls.login(username, password);
if(user == null)
return new LoginView();
else
{
req.setAttribute("appUser", user);
req.getSession().setAttribute("appUser", user);
return new HomeView();
}
}
HomeView class:
public class HomeView extends View
{
public HomeView()
{
super("home.ftl");
}
}
Freemarker template home.ftl:
<html>
<head>
</head>
<body>
<#if appUser??>
<h1>Welcome, ${appUser.username}!</h1>
<#else>
AppUser is not found in the session
</#if>
</body>
</html>
run() method of the class that extends Service:
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
//setting session handler
environment.setSessionHandler(new SessionHandler());
environment.addResource(new LoginResource());
}
The problem is that when i make a successful login attempt it seems that my user is not saved in the session. Don't know how to resolve this.