[英]What is wrong with my code (GWT)
I'm creating a login application on Eclipse using Google Web Toolkit(GWT). 我正在使用Google Web Toolkit(GWT)在Eclipse上创建一个登录应用程序。 The code checks for the username and password and if its correct, it shows the o/p as welcome. 代码检查用户名和密码,如果正确,则显示o / p为welcome。 Still after compiling it is giving me errors.I'm sharing both code and the error message. 编译后仍然给我错误。我正在共享代码和错误消息。 Please help me out. 请帮帮我。
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);
}
}
Error Message is---------- 错误信息是----------
Compiling module com.vin.HelloWorld Exception in thread "UnitCacheLoader" java.lang.RuntimeException: Unable to read from byte cache at com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:166) at com.google.gwt.dev.util.DiskCacheToken.readObject(DiskCacheToken.java:87) at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ..............and many more like this.... Please help me out 编译模块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)..............以及更多这样的.. .. 请帮帮我
Try something like following for Server side : 尝试使用以下服务器端:
UserService.java UserService.java
@RemoteServiceRelativePath("userService")
public interface UserService extends RemoteService {
String loginUser(String username,String password);
}
UserServiceAsync.java UserServiceAsync.java
public interface UserServiceAsync {
void loginUser(String username, String password, AsyncCallback<String> callback);
}
UserServiceImpl.java 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
}
}
Follow Communicate with a Server in GWT and the Anatomy of service 遵循GWT中的服务器通信和服务剖析
For Client Side : 对于客户端:
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);
}
}
You can not put DB related code in Entry point class, you need to call GWT-RPC on click method. 你不能把DB相关的代码放在入口点类中,你需要在click方法上调用GWT-RPC。
Actually this EntryPoint class would be compiled by GWT processor and it will create javascript in output which going to run in browser. 实际上这个EntryPoint类将由GWT处理器编译,它将在输出中创建javascript,将在浏览器中运行。 So there is no justification you can call db in javascript. 因此没有理由可以在javascript中调用db。
GWT-RPC is asynchronous call which code reside in server. GWT-RPC是异步调用,代码驻留在服务器中。 Here you can write all business logic, db interactivity etc. 在这里,您可以编写所有业务逻辑,数据库交互等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.