Revision 1 as of 2009-12-15 00:34:42

Clear message
Locked History Actions

guice/Manual/Integration/WebandServlets/ServletRegexKeyMapping

Regular Expressions

You can also map servlets (or filters) to URIs using regular expressions:

This will map any URI containing the text "ajax" in it to MyAjaxServlet. Such as:

Initialization Parameters

Servlets (and filters) allow you to pass in string values using the <init-param> tag in web.xml. Guice Servlet supports this using a Map<String, String> of name/value pairs. For example, to initialize MyServlet with two parameters coffee="Espresso" and site="google.com" you could write:

  • Map<String, String> params = new HashMap<String, String>(); params.put("coffee", "Espresso"); params.put("site", "google.com");

  • ..

And the ServletConfig object passed to MyServlet will contain the appropriate input parameters when using getInitParams(). The same syntax is available with filters. Binding Keys

You can also bind keys rather than classes. This lets you hide implementations with package-local visbility and expose them using only a Guice module and an annotation:

  • filter("/*").through(Key.get(Filter.class, Fave.class));

Where Filter.class refers to the Servlet API interface javax.servlet.Filter and Fave.class is a custom binding annotation. Elsewhere (in one of your own modules) you can bind this filter's implementation:

  • bind(Filter.class).annotatedWith(Fave.class).to(MyFilterImpl.class);

See the User's Guide for more information on binding and annotations. Injecting the injector

Once you have a servlet or filter injected for you using ServletModule, you can access the injector at any time by simply injecting it directly into any of your classes:

@Singleton public class MyServlet extends HttpServlet {

  • @Inject private Injector injector;
  • ..

}

// elsewhere in ServletModule serve("/myurl").with(MyServlet.class);

This is often useful for integrating third party frameworks that need to use the injector themselves.