Sunday, September 5, 2010

GWT - database, authentication, session

Hi,

i would like to create a login panel, when i must enter username and
password. Next I check if the data are correct. When the data are
correct I reload home page and I see message "Hi <username>! etc...".
When I refresh site i logged on.

Now I have something like this:
Test.java (default):
public void onModuleLoad() {
// Lets add a grid to hold all our widgets
Grid grid = new Grid(4, 2);
//Set the error label
grid.setWidget(0,1, lblError);
//Add the Label for the username
grid.setWidget(1,0, new Label("Username"));
//Add the UserName textBox
grid.setWidget(1,1, txtLogin);
//Add the label for password
grid.setWidget(2,0, new Label("Password"));
//Add the password widget
grid.setWidget(2,1, txtPassword);
//Create a button
Button btnLogin=new Button("login");
btnLogin.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
sendNameToServer();
}
});

//Add the Login button to the form
grid.setWidget(3,1, btnLogin );
RootPanel.get().add(grid);
}

private void sendNameToServer() {
errorLabel.setText("");
// First, we validate the input.
String login = txtLogin.getText();
String pass = txtPassword.getText();

// Then, we send the input to the server.
getService().greetServer(login, pass, GWT.getHostPageBaseURL(),
new AsyncCallback<LoginInfo>() {
public void onFailure(Throwable error) {
// Show the RPC error message to the user
errorLabel.setText("Error");
}

public void onSuccess(LoginInfo result) {
loginInfo = result;
if(loginInfo.isLoggedIn()) {
RootPanel.get().clear();
errorLabel.setText("You are logged");
RootPanel.get().add(errorLabel);
} else {
errorLabel.setText("Login failed");
}

}
});
}

private GreetingServiceAsync getService() {
GreetingServiceAsync greetingService = (GreetingServiceAsync) GWT
.create(GreetingService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) greetingService;
endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "login");

return greetingService;
}

GreetingService.java (client)
public interface GreetingService extends RemoteService {
public LoginInfo greetServer(String login, String pass, String
requestUri);
}

GreetingServiceAsync.java (client)
public interface GreetingServiceAsync {
public void greetServer(String login, String pass, String requestUri,
AsyncCallback<LoginInfo> async);
}


GreetingServiceImpl.java (server)
public class GreetingServiceImpl extends RemoteServiceServlet
implements GreetingService {

/**
*
*/
private static final long serialVersionUID = 1L;

public LoginInfo greetServer(String login, String pass, String
requestUri) {
LoginInfo loginInfo = new LoginInfo();

Connection myCon;
Statement myStmt;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
myCon = DriverManager.getConnection("jdbc:mysql://localhost/
isr", "root","root");
myStmt = myCon.createStatement();
ResultSet result = myStmt.executeQuery("SELECT * FROM users
WHERE login=\""+login+"\" and pass=\""+pass+"\" ORDER by login ASC
limit 1");
loginInfo.setLoggedIn(true);
result.last();
if (result.getRow()>0) {
loginInfo.setLoggedIn(true);
loginInfo.setEmailAddress(result.getString("mail"));
loginInfo.setLogoutUrl(requestUri);
loginInfo.setNickname(result.getString("login"));
myCon.close();
} else {
loginInfo.setLoggedIn(false);
loginInfo.setLoginUrl(requestUri);
}
}
catch (Exception sqlEx){
loginInfo.setLoggedIn(false);
}
return loginInfo;
}
}


Please tell me what i should do to be logged when I refresh page?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment