How to minify servlet declarations for gwt-rpc in web.xml?

Sorry, I'm still new to GWT. I noticed that as my project grew, there are many, many, many rpc servlet declarations in the web.xml file. For one * 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 you have 30 * ServiceImpl class, they can take about 200 lines in web.xml for rpc calls. So I would like to know

  • Is the web.xml file the only place to declare rpc servlets?
  • Can anyone skip declarative styles (I mean via "@" annotations, etc.)?
+3


source to share


2 answers


GWT works really 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 your project structure. If your project is deployed to the root of your server, you will find your servlet there (with the path given above urlPatterns). However, if you deployed your project under your own URI, you will have to add it to urlPattern.

+3


source


If you are using Guice, this case can be easily solved using ServletModule . In this module, you can programmatically define (and test in JUnit) all of your RPC servlets and filters.

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);
  }
}

      

+2


source







All Articles