3

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) ?
Cataclysm
  • 7,592
  • 21
  • 74
  • 123

2 Answers2

3

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.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
ArcTanH
  • 384
  • 2
  • 10
  • Thank you for your answer. But I don't catch yet. How to declare with **@WebServlet** ? How about my service,ServiceAnync,ServiceImpl classes ? Can you support me with code example ? – Cataclysm May 15 '15 at 09:54
  • I always get 404. Where to put **@WebServlet** ? – Cataclysm May 15 '15 at 10:18
  • 1
    what is the version of web.xml? As I recal, it should be over 3.0 to use annotations – Heisenberg May 15 '15 at 10:56
  • I updated the example above for clarification. hope this helps. – ArcTanH May 15 '15 at 10:59
  • 1
    @Heisenberg good point. That depends on the server you use. e.g. Tomcat needs Servlet 3.0 spec see http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat-7 – ArcTanH May 15 '15 at 11:15
  • Also, I'm almost certain it won't work with the DevMode's embedded Jetty server. – Thomas Broyer May 15 '15 at 12:31
  • As sir @ThomasBroyer said , I belive it won't work with embedded Jetty server. So , I think I always get 404. I would like to use Jetty server only in developing times. So , I can't use this way (via @WebServlet). But thank you for your help. *I think running with tomcat server may take long time rather than embedded Jetty Server.* – Cataclysm May 15 '15 at 18:03
  • 1
    Running with tomcat (or others) does not take long time, but can make debugging very tricky, there's no 'edit, refresh and be happy' anymore. I never testet these annotations with Jetty, but in case they don't work (and to answer your question completely), yes, web.xml is the only place to declare your rpc-mappings. – ArcTanH May 18 '15 at 07:52
2

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);
  }
}
xRomZak
  • 176
  • 3
  • 10