This page describes how to Invoke Server Side Code to retrieve data from the server side and display the results in a JavaScript pop-up.
Background
GWT generates JavaScript code that makes a call to server side components thru the Servlet Interface. The Service Implementation class extends the “RemoteServiceServlet” class. This allows GWT to respond to requests made from the GWT generated Javascript.
Requirements
- Maven 2
- M2 Eclipse plugin
- Eclipse GWT plugin
- Successful completion of my previous post
Procedure
Before we begin we need to create directories.
mkdir -p src/main/java/com/test/server
Create the Interface
First you create a an interface that can be used by the client. Maven generates a client side Interface based this. The interface class should be in the module’s client package.
vi src/main/java/com/test/client/GreetingService.java
package com.test.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String input) throws IllegalArgumentException;
}
Create the Implementation
The implementation class should be in the module’s server package.
vi src/main/java/com/test/server/GreetingServiceImpl.java
package com.test.server;
import com.test.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
String serverInfo = getServletContext().getServerInfo();
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
return "Hello, " + input + "! I am running " + serverInfo
+ ".It looks like you are using:" + userAgent;
}
}
Define the Servlet
Add the servlet to the web.xml.
vi src/main/webapp/WEB-INF/web.xml
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>com.test.server.GreetingServiceImpl</servlet-class>
</servlet>
<!-- Servlet-Mapping -->
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>com.test.Matrix/greet</url-pattern>
</servlet-mapping>
Change the EntryPoint
Make the changes described in the following patch file by hand to the existing file.
vi src/main/java/com/test/client/Matrix.java
--- src/main/java/com/test/client/Matrix.java
+++ src/main/java/com/test/client/Matrix.java
@@ -5,15 +5,26 @@
+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.RootPanel;
public class Matrix implements EntryPoint {
-
+ private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
+
public void onModuleLoad() {
Button button = new Button("Send", new ClickHandler() {
public void onClick(ClickEvent event) {
- Window.alert("Hello World!");
+ greetingService.greetServer("World", new AsyncCallback<String>() {
+ public void onFailure(Throwable caught) {
+ // Show the RPC error message to the user
+ Window.alert("Remote Procedure Call - Failure");
+ }
+
+ public void onSuccess(String result) {
+ Window.alert(result);
+ }
+ });
}
});
button.setStyleName("sendButton");
Test the application
- Right click on the project in Eclipse and Refresh.
- right click on the project -> Maven -> Update Project Configuration.
Everything should compile fine in eclipse.
Next go to the command prompt and type the following in the project’s base folder.
mvn compile gwt:run
After clicking the button on the screen a JavaScript alert box will open returning text from the server side. You can use Firefox FireBug plugin to verify the server side communication.
Troubleshooting
- Is the Service interface in the client package of the module’s package?
- Is the service implementation in the server package of the module’s package?
- Does the web.xml file specify the servlet class implementaion?
- Does the web.xml specify the url-pattern that points to the gwt.xml file name/[remoteServiceRelativePath]. (without gwt.xml extension)
For Example: com/test/AlbumSearch.gwt.xml and a @RemoteServiceRelativePath(“musicsearch”) on the interface will translate to:<url-pattern>com.test.AlbumSearch/musicsearch</url-pattern>