运行GWT应用程序时出现异常

时间:2012-10-05 10:16:01

标签: java gwt exception rpc

我已经构建了我的第一个GWT应用程序。没有编译错误也没有运行时错误。但是,当应用程序加载到浏览器中时(使用Interner Explorer)并输入用户名密码字段来验证用户,它会抛出异常。使用GWT-RPC方法,提供了整个代码和接口。 我正在使用 HSQL 进行数据库连接(后端)。

------------------代码(客户)

package com.vin.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
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.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class HelloWorld implements EntryPoint{
    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 PasswordTextBox t_passwrd=new PasswordTextBox();
        click.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent ev) {
            String temp_user=t_name.getText();
            String temp_pass=t_passwrd.getText();
                 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");
//                         Window.open("http://127.0.0.1:8888/ExWid.html?gwt.codesvr=127.0.0.1:9997", "Dem", null);
                     }
                 });
            }
        });
        RootPanel.get().add(name);
        RootPanel.get().add(t_name);
        RootPanel.get().add(passwrd);
        RootPanel.get().add(t_passwrd);
        RootPanel.get().add(click);
}
}

-----------------------------客户端界面(1)

package com.vin.client;

import com.google.gwt.user.client.rpc.RemoteService;

public interface UserService extends RemoteService {
    public String loginuser(String username, String password);
}

----------------------------客户ASYNC接口

package com.vin.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface UserServiceAsync {
    public void loginuser(String username, String password, AsyncCallback<String> callback);
}

--------------------------客户USERSERVICE(服务器)的实现......数据库连接

package com.vin.server;

import java.sql.DriverManager;
import java.sql.ResultSet;
import com.google.gwt.dev.generator.ast.Statement;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.vin.client.UserService;

public class UserServiceImpl extends RemoteServiceServlet implements UserService{
    private static final long serialVersionUID = 1L;

    public String loginuser(String username,String password) {
        try {
            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(username.equals(user) && password.equals(pass)) {
                Window.alert("success");
            }
        }
    catch (Exception ae) {}
        return "success";
    }
}

------------------我试图验证用户时的异常列表

  

15:22:54.583 [错误] [helloworld]未捕获的异常逃脱   com.google.gwt.event.shared.UmbrellaException:一个或多个例外   抓住了,在UmbrellaException中看到全套#getCauses       在com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)       在com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)       在com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116)       在com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)       在com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)

还有更多像这样的人。

2 个答案:

答案 0 :(得分:2)

com.google.gwt.user.client.Window类提供对浏览器窗口的方法,属性和事件的访问。所以你不能在Serverside中使用它。最好在需求满足时返回String "success",否则返回Exception,以便在客户端被onFailure捕获。

答案 1 :(得分:1)

我认为你不能在服务器端使用Window.alert(在UserServiceImpl类中)。可能有许多客户端和服务器无法知道它所针对的客户端。

但我不确定是否会导致此错误。

相关问题