Saturday, November 3, 2012

Re: RPC fails

Server side:

package main.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import main.client.GreetingService;
import main.client.User;
import main.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class MySQLConnection extends RemoteServiceServlet implements
GreetingService {
private Connection conn = null;
private String status;
private String url = "jdbc:mysql://localhost/testdb";
private String user = "root";
private String pass = "root";
public MySQLConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
System.out.println("Failed to connect!");
//NEVER catch exceptions like this
}

}
public User greetServer(String user, String pass) throws IllegalArgumentException {
System.out.print("inside greetServer");
User u = new User("user", "pass");
try {
PreparedStatement ps = conn.prepareStatement("select readonly * from users where username = \"" + user + "\" AND " + "password = \"" + pass + "\"");
ResultSet result = ps.executeQuery();
while (result.next()) {
u = new User(result.getString(1), result.getString(2));
}
result.close();
ps.close();
} catch (SQLException sqle) {
System.out.println("user not found!");
}
return u;
}

/**
* Escape an html string. Escaping data received from the client helps to
* prevent cross-site script vulnerabilities.
* @param html the html string to escape
* @return the escaped string
*/
private String escapeHtml(String html) {
if (html == null) {
return null;
}
return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
}
}


On Saturday, November 3, 2012 2:01:20 PM UTC-7, Sevak Asadorian wrote:
This is the error I get:

Nov 03, 2012 8:51:23 PM com.google.appengine.tools.development.LocalResourceFileServlet doGet
WARNING: No file found for: /mate/MySQLConnection

gwt.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><module rename-to="mate">
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name="com.google.gwt.user.User"/>

  <!-- Inherit the default GWT style sheet.  You can change       -->
  <!-- the theme of your GWT application by uncommenting          -->
  <!-- any one of the following lines.                            -->
  <inherits name="com.google.gwt.user.theme.clean.Clean"/>
  <!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
  <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
  <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

  <!-- Other module inherits                                      -->

  <!-- Specify the app entry point class.                         -->
  <entry-point class="main.client.Mate"/>

  <servlet class="main.server.MySQLConnection" path="/MySQLConnection"/>

</module>

Mate.java

...
private class AuthenticationHandler<T> implements AsyncCallback<User> {
public void onFailure(Throwable ex) {
Window.alert(ex.getMessage());
RootPanel.get().add(new HTML("RPC call failed"));
// Convenient way to find out which exception was thrown.
try {
throw ex;
} catch (IncompatibleRemoteServiceException e) {
System.out.println(e);
// this client is not compatible with the server; cleanup and refresh the 
// browser
} catch (InvocationException e) {
System.out.println(e);
// the call didn't complete cleanly
} catch (Throwable e) {
System.out.println(e);
// last resort -- a very unexpected exception
}
}
public void onSuccess(User result) {
//do stuff on success with GUI, like load the next GUI element
RootPanel.get().add(new HTML("Success!"));
}
}
...

Button named "OK" which calls AuthenticationHandler

OK.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
AsyncCallback<User> callback = new AuthenticationHandler<User>();
rpc.greetServer(usernameBox.getText(), passwordBox.getText(), callback);
}
});

Not sure what else to supply. Please let me know what you would like to see and I will gladly paste it in here.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/yFpWJ8HRX7oJ.
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