How do I define servlet init-parameters in my .gwt.xml file?

370 views
Skip to first unread message

laredotornado

unread,
Feb 14, 2012, 2:42:38 PM2/14/12
to Google Web Toolkit
Hi,

I'm using GWT 2.4. How do I define servlet init parameters in my
module (.gwt.xml) file when defining a servlet? I'm writing one for
testing, but apparently the XML I copied online

<servlet>
<servlet-name>SaveServlet</servlet-name>
<servlet-class>com.myco.clearing.web.SaveXmlServlet</servlet-class>
<init-param>
<param-name>cacheServiceLocatorClass</param-name>
<param-value>com.myco.clearing.product.test.MockCacheServiceLocator</
param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SaveServlet</servlet-name>
<url-pattern>/save</url-pattern>
</servlet-mapping>

isn't valid because I'm getting a lot of errors like "Cannot find
'path' attribute" and what not. Any guidance is appreciated, - Dave

skippy

unread,
Feb 14, 2012, 4:04:27 PM2/14/12
to Google Web Toolkit
Dave:

You don't use the Module.gwt.xml file to define servlets.
The HTTP Servlet spec and pure java doesn't know about that module.gwt
xml.

Simply use you normal Web.xml to add servlets for the GWT RPC service
calls.
The standare onload stuff work just a before.

remember, GWT is compiling you Java Script files to run the UI.

HTH!

laredotornado

unread,
Feb 14, 2012, 5:27:57 PM2/14/12
to Google Web Toolkit
I need this for my GWT test files -- those extending GWTTestCase. GWT
test case requires you to define a .gwt.xml file where you define
servlets. As far as I can tell, GWTTestCase can't read web.xml files.

So, how do I define init parameters for my servlet that can then be
processed by GwtTestCase?

- Dave

Thomas Broyer

unread,
Feb 15, 2012, 4:33:37 AM2/15/12
to google-we...@googlegroups.com
Create a subclass of your servlet for your tests, where you set these values (in your Java code). Something like (using a Map so the pattern is easily reusable when you have more than one such parameter):

public class MyServletForTests extends MyServlet {
  private static final Map<String, String> INIT_PARAMETERS;
  static {
    Map<String, String> initParameters = new HashMap<String,String>();
    initParameters.put("cacheServiceLocatorClass", "com.myco.clearing.product.test.MockCacheServiceLocator");
    INIT_PARAMETERS = Collections.unmodifiableMap(initParameters);
  }

  @Override
  public void init(final ServletConfig config) {
    super.init(new ServletConfig() {
      public String getServletName() { return config.getServletName(); }
      public ServletContext getServletContext() { return config.getServletContext(); }
      public String getInitParameter(String name) {
        // you could also check if the map contains the key and fallback to the super implementation otherwise; it would complicate getInitParameterNames though
        return INIT_PARAMETERS.get(name);
      }
      public Enumeration<String> getInitParameterNames() {
        return Collections.enumeration(INIT_PARAMETERS.keySet());
      }
    }
  }
}

laredotornado

unread,
Feb 15, 2012, 3:19:10 PM2/15/12
to Google Web Toolkit
Thanks for this creative solution. - Dave
Reply all
Reply to author
Forward
0 new messages