Pages

Monday, May 11, 2009

Login Suggest Box With Rpc

Share it Please

In this blog we will see how can we write a login Page where when user enteres a letter we will show him the available user already registered in the application using a suggest box.for this we will be writing a Gwt Rpc where we get the available user names form the server side. We can extend the functionality by making a call to data base .But here I will limit the functionaliy to the server code.

Now lets write the PasswordServer interface

public interface PasswordService extends RemoteService {

public LoginData getValidPassword(LoginData data);

public LoginData getLoginUserNames();

public static class getUtil{

public static PasswordServiceAsync getInstance() {

PasswordServiceAsync async=(PasswordServiceAsync)GWT.create(PasswordService.class);

((ServiceDefTarget)async).setServiceEntryPoint(GWT.getModuleBaseURL()+"/callToServer");

return async;

}

}

}

You can see I have written a static inner class to make a instance of PasswordServiceAsync to get the instance which we will use in making a call to server.

Now the passwordServiceAsync interface

public interface PasswordServiceAsync {

public void getValidPassword(LoginData data,AsyncCallback callback);

public void getLoginUserNames(AsyncCallback callback);

}

Now a login data class which we will use to send and get the data from server side

public class LoginData implements IsSerializable {

public String userName;

public String password;

public ArrayList userLoginNames=new ArrayList();

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public ArrayList getUserLoginNames() {

return userLoginNames;

}

public void setUserLoginNames(ArrayList userLoginNames) {

this.userLoginNames = userLoginNames;

}

}

This class implements isSerializable which makes the class object to move through wire.

Until now we have seen class which comes in the client side packages. Now we will look the implementation of passwordSerivceImpl class which implements the PasswordService and extends the RemoteServiceServlet.

public class PasswordServiceImpl extends RemoteServiceServlet implements

PasswordService {

public LoginData getValidPassword(LoginData data) {

return data;

}

public LoginData getLoginUserNames() {

ArrayList userLoginList=new ArrayList();

userLoginList.add("jagadesh");

userLoginList.add("tiru");

userLoginList.add("sridhar");

userLoginList.add("rama krishna");

userLoginList.add("naveen");

userLoginList.add("arvind");

userLoginList.add("madhavi");

userLoginList.add("arun");

userLoginList.add("kamal");

LoginData data=new LoginData();

data.setUserLoginNames(userLoginList);

return data;

}

}

In the above class I just made a arraylist with some values and send them back to client.these names are nothing but the registered users .we can extend here by making a call to database and get reqults.

now lets write a special class which comes in the client package called “loginPage.java”

public class LoginPage extends Composite {

public SuggestBox userName;

public PasswordTextBox password;

public AsyncCallback callback;

private FlexTable panel;

private Button sendData;

public static LoginListener listener;

public static MultiWordSuggestOracle userLoginNames;

static {

userLoginNames=new MultiWordSuggestOracle();

AsyncCallback callback=new AsyncCallback() {

public void onFailure(Throwable caught) {

}

public void onSuccess(LoginData data) {

for(int i=0;i().size();i++) {

userLoginNames.add(String.valueOf(data.getUserLoginNames().get(i)));

}

}

};

PasswordService.getUtil.getInstance().getLoginUserNames(callback);

}

public LoginPage(LoginListener myListener) {

this.listener=myListener;

panel=new FlexTable();

userName=new SuggestBox(userLoginNames);

password=new PasswordTextBox();

sendData=new Button("Send Data");

panel.setWidget(0,0,new Label("User Name"));

panel.setWidget(0,1,userName);

panel.setWidget(1,0,new Label("Password"));

panel.setWidget(1,1,password);

panel.setWidget(2,0,sendData);

callback=new AsyncCallback() {

public void onFailure(Throwable caught) {

listener.onLoginFailure(caught);

}

public void onSuccess(LoginData loginData) {

listener.onLoginSuccess(loginData);

}

};

sendData.addClickListener(new ClickListener() {

public void onClick(Widget sender) {

LoginData data=new LoginData();

data.setUserName(userName.getText());

data.setPassword(password.getText());

PasswordService.getUtil.getInstance().getValidPassword(data, callback);

}

});

initWidget(panel);

}

public interface LoginListener{

public void onLoginSuccess(LoginData loginData);

public void onLoginFailure(Throwable error);

}

}

U can see that I have written a interface called “loginListener” .The constructor of the login page takes this as an argument . this contains 2 method onLoginSuccess() which gets called when login is success and OnLoginFailure() which gets called on LoginFailure().you can also see that the above class contains a static block which gets executed before constructor is executed . in which I made a call to server to get values which will be shown in the suggest box.

Now lets see how the OnmoduleLoad() is written ,

public class SampleApp implements EntryPoint {

public TextBox userName;

public PasswordTextBox password;

public AsyncCallback callback;

public void onModuleLoad() {

LoginPage page=new LoginPage(new LoginListener() {

public void onLoginFailure(Throwable error) {

}

public void onLoginSuccess(LoginData loginData) {

Window.alert(loginData.getUserName());

}

});

RootPanel.get().add(page);

}//OnModuleLoad() Close

}

You can see the onModuleload() where I made a instancce of LoginPage by sending an argument of LoginListener . the methods are invoked depending on the server status.






No comments :

Post a Comment