3
votes

Sorry I am still a beginner in GWT. I have noticed that when my project was grow up , the declarations of servlets for rpc in web.xml file are many, many and many. For a single *ServiceImpl class , we need to define in web.xml as

<servlet>
    <servlet-name>greetServlet</servlet-name>
    <servlet-class>com.my.testing.server.GreetingServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>greetServlet</servlet-name>
    <url-pattern>/testing/greet</url-pattern>
</servlet-mapping>

If if have 30 *ServiceImpl class, they may take about 200 lines in web.xml for rpc calls. So , I would like to know

  1. Is web.xml file the only place to declare rpc servlets ?
  2. Has someways to skip declarative styles (I mean via annotations '@' etc) ?
2

2 Answers

3
votes

GWT works pretty well without these declarations in web.xml, using Annotations:

/*
 * this is your server-side rpc-implementation
 */
@WebServlet(name = "YourService", urlPatterns = {"/path/to/yourservice"})
public class YourServiceImpl extends RemoteServiceServlet implements YourService {

  public void doSomething() {
    //some code
  }
}

/*
 * this is the corresponding rpc-interface
 */
@RemoteServiceRelativePath("path/to/yourservice")
public interface YourService implements RemoteService {

  void doSomething();
}

The resulting path to your servlet depends on you project structure. If your project is deployed in your servers root, you will find your servlet there (with the path you specified with urlPatterns above). However, if you deployed your project under its own URI, you will have to prepend this to the urlPattern.

2
votes

If you use Guice, this case can be easy solved using ServletModule. In this module you may programmatically define (and test in JUnit) all your RPC servlets and filters as well.

Example:

public class WebModule extends ServletModule {

  @Override
  protected void configureServlets() {
    // configure filters
    filter("/*").through(CacheControlFilter.class);
    filter("/*").through(LocaleFilter.class);

    // bind XSRF servlet
    bind(XsrfTokenServiceServlet.class).in(Singleton.class);
    serve("/gwt/xsrf").with(XsrfTokenServiceServlet.class);

    // configure servlet mapping
    serve("path/to/servlet").with(LoginServiceImpl.class);
  }
}