My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ServletRegexKeyMapping  
Advanced topics: regex mapping, init params, etc.
Updated Oct 16, 2011 by sberlin

Regular Expressions

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

    serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)

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

  • http://www.google.com/ajax.html
  • http://www.google.com/content/ajax/index
  • http://www.google.com/it/is-totally-ajaxy

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

  ...
      serve("/*").with(MyServlet.class, params)

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.

Comment by alist...@entsia.com, Aug 9, 2011

The link the the User's Guide is not publicly accessible...

Comment by owen.k.b...@gmail.com, Sep 17, 2011

Is there any way to bootstrap java classes that aren't part of the servlet container, e.g. Classes in the business layer that are not Servlets or Filters?

Is there any way to make those classes injectable without injecting the injector?

Comment by lukasz.d...@gmail.com, Oct 7, 2011

Is there any way to make something like this work:

serve("/someurl").with(MyServlet?.class, someParams) serve("/otherurl").with(MyServlet?.class, otherParams)

problem is that after first binding went off someParams are injected into Singleton MyServlet? servlet, and then otherParams are ignored.

Comment by rosolovs...@gmail.com, Mar 18, 2012

Servlet (MyServlet?.class in your case) is a Singleton and created once, if you want to path there context (request for example) related parameters you should use provider with @RequestScoped?.

Or you can create a Filter for both of yours endpoints and call setParameter, then you can find you parameter in Servlet by calling getParameter instead of getInitParameter;


Sign in to add a comment
Powered by Google Project Hosting