HI, maybe this peace of code can Help :) :
You can use two modules/entries, one for the login other after login
on login
Client side:
public void onModuleLoad() {
this.setLoginPanel();
LogUtils.info("Showing Login page");
loginButton = new Button("Login");
loginButton.addListener(new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
userAuthentication();
}
});
.....
private void userAuthentication() {
if (this.userNameField.getValueAsString().equals(""))
Window.alert("username must not be empty.");
else {
loginService = GWT.create(LoginService.class);
String username = this.userNameField.getValueAsString();
String password = this.passwordField.getValueAsString();
this.loginService.login(username, password,
new AsyncCallback<LoginResponse>() {
public void onFailure(Throwable caught) {
Window.alert("server side failure: " + caught);
}
public void onSuccess(LoginResponse result) {
if (result.isLoginSuccess()){
Window.Location.replace("./../Main.html?gwt.codesvr=
127.0.0.1:9997");
}
else Window.alert("username or password invalid.");
}
});
}
}
ON SERVER SIDE (the login method):
public LoginResponse login(String username, String password) {
LoginPService loginService = ServiceLocator.getLoginService();
Person person = null;
try {
ManageLogs.info("Try to login for user: "+username);
person = loginService.getUserByUsername(username);
if (person == null){
return new LoginResponse(false, false);
} else if (!loginService.checkPassword(password)){
return new LoginResponse(false, false);
}
} catch (Throwable e) {
return new LoginResponse(false, false);
}
ManageLogs.info("Login sucessful for user: "+username);
LoginResponse response = new LoginResponse();
response.setLoginSuccess(true);
/*Creates de session*/
MainSession padroesSession = mainSession.getInstance();
mainSession.setRequest(getThreadLocalRequest());
mainSession.setUser(person);
return response;
}
THE MainSession
private static MainSession mainSession=null;
public static MainSession getInstance(){
if(mainSession == null){
mainSession = new MainSession();
return mainSession;
} else {
return mainSession;
}
}
private MainSession(){
}
private static final String USER_SESSION = "userSession";
private HttpServletRequest request = null;
private HttpSession session = null;
private String sessionId = "";
public Person getUser(){
if(null == session) return null;
return session.getAttribute(USER_SESSION) != null ?
(Person)session.getAttribute(USER_SESSION) : null;
}
public HttpSession getSession(){
return session;
}
public void invalidate(){
if(request!=null)
if(request.getSession(false)!= null)
request.getSession(false).invalidate();
if(null != session){
session.invalidate();
session = null;
}
setSessionId(null);
}
public void setUser(Person user){
if(null == user){
if(session!=null) session.removeAttribute(USER_SESSION);
return;
}
if(null != request)
this.session = request.getSession(true);
if(session!=null){
session.setAttribute(USER_SESSION, user);
setSessionId(session.getId());
}
}
public String getId(){
return request.getSession(false).getId();
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
....
ON THE SECOND ENTRY
public void onModuleLoad() {
LogUtils.info("Loading Padroes Module");
MainSessionServiceAsync mainSessionService = GWT.create(MainSessionService.class);
AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
@Override
public void onFailure(Throwable caught) {
LogUtils.debug("no session available");
Window.Location.replace("./../Login.html");
}
@Override
public void onSuccess(Boolean result) {
if(!result){
LogUtils.debug("no session available");
Window.Location.replace("./../Login.html");
return;
}
LogUtils.info("creating new Session Time Out for this session");
/* initialize timers for session time out control */
new SessionTimeOutControl();
/* Creates de layout +/
doLayout();
}
};
try{
mainSessionService.isValidSession(callback);
}catch(Exception e){
e.printStackTrace();
}
public void doLayout(){
AsyncCallback<PageConfiguration[]> callback = new AsyncCallback<PageConfiguration[]>(){
@Override
public void onFailure(Throwable caught) {
LogUtils.debug("server side error on getting PageConfiguration");
Window.Location.replace("./../Login.html");
}
@Override
public void onSuccess(PageConfiguration[] result) {
mainPanel.setStyleName("panel-border");
mainPanel.setFrame(true);
.......
Hope it helps :)