我正在使用Google Web Toolkit(GWT)在Eclipse上创建登录应用程序。代码检查用户名和密码,如果正确,则显示o / p为welcome。编译后仍然给我错误。我正在共享代码和错误消息。请帮帮我。
package com.vin.client;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dev.generator.ast.Statement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
public class HelloWorld implements EntryPoint{
public void onModuleLoad() {
Button click=new Button("Click Here");
Label name=new Label("Enter Name");
Label passwrd=new Label("Enter Password");
final TextBox t_name=new TextBox();
final TextBox t_passwrd=new TextBox();
click.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent ev) {
try {
String temp_user=t_name.getText();
String temp_pass=t_passwrd.getText();
java.sql.Connection con = null;
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");
Statement st=(Statement) con.createStatement();
ResultSet rs=((java.sql.Statement) st).executeQuery("select username,password from lgfrm");
String user=rs.getString(1);
String pass=rs.getString(2);
if(temp_user.equals(user) && temp_pass.equals(pass)) {
Window.alert("Welcome");
}
else {
Window.alert("Please enter valid details");
}
}
catch (Exception ae) {}
}
});
RootPanel.get().add(name);
RootPanel.get().add(t_name);
RootPanel.get().add(passwrd);
RootPanel.get().add(t_passwrd);
RootPanel.get().add(click);
}
}
错误信息是----------
编译模块com.vin.HelloWorld线程中的异常 “UnitCacheLoader”java.lang.RuntimeException:无法从字节读取 缓存在 com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:166) 在 com.google.gwt.dev.util.DiskCacheToken.readObject(DiskCacheToken.java:87) at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ........还有更多像这样...... 请帮帮我
答案 0 :(得分:2)
尝试使用以下服务器方面的内容:
<强> UserService.java 强>
@RemoteServiceRelativePath("userService")
public interface UserService extends RemoteService {
String loginUser(String username,String password);
}
<强> UserServiceAsync.java 强>
public interface UserServiceAsync {
void loginUser(String username, String password, AsyncCallback<String> callback);
}
<强> UserServiceImpl.java 强>
public class UserServiceImpl extends RemoteServiceServlet implements UserService {
public String loginUser(String username, String password){
//database interaction
return "result"; //return success or failure depending upon logic
}
}
关注Communicate with a Server in GWT和Anatomy of service
对于客户端:
public class HelloWorld implements EntryPoint{
//(1) Create the client proxy.
private UserServiceAsync userService = (UserServiceAsync) GWT.create(UserService.class);
public void onModuleLoad() {
Button click=new Button("Click Here");
Label name=new Label("Enter Name");
Label passwrd=new Label("Enter Password");
final TextBox t_name=new TextBox();
final TextBox t_passwrd=new TextBox();
click.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent ev) {
String temp_user=t_name.getText();
String temp_pass=t_passwrd.getText();
/// (2) Create an asynchronous callback and Make the call
userService.loginUser(temp_user, temp_pass, new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
Window.alert("Please enter valid details");
}
public void onSuccess(String result) {
Window.alert("Welcome");
}
});//end of service call
});//end of clickhandler
RootPanel.get().add(name);
RootPanel.get().add(t_name);
RootPanel.get().add(passwrd);
RootPanel.get().add(t_passwrd);
RootPanel.get().add(click);
}
}
答案 1 :(得分:1)
你不能把DB相关的代码放在入口点类中,你需要在click方法上调用GWT-RPC。
实际上这个EntryPoint类将由GWT处理器编译,它将在输出中创建将在浏览器中运行的javascript。因此没有理由可以在javascript中调用db。
GWT-RPC是异步调用,代码驻留在服务器中。在这里,您可以编写所有业务逻辑,数据库交互等。