http://code.google.com/p/google-guice/wiki/Servlets

Start by placing GuiceFilter at the top of your .web.xml file:

  <filter>

    <filter-name>guiceFilter</filter-name>

    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>guiceFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

<listener>

  <listener-class>com.example.MyGuiceServletConfig</listener-class>

</listener>

The name of the filter isn’t important

com.google.inject.servlet.GuiceFilter matters; it’s the name of the instantiated class

The url-pattern sends everything to GuiceFilter

http://code.google.com/p/google-guice/wiki/ServletModule

public class MyGuiceServletConfig extends GuiceServletContextListener {

  @Override

  protected Injector getInjector() {

    return Guice.createInjector(new ServletModule());

  }

}

Extend GuiceServletContextListener to do some setup

Add it to web.xml so it’s called on startup

You’ll want to replace new ServletModule() with your own preconfigured module.

Guice.createInjector(..., new ServletModule() {

     @Override

     protected void configureServlets() {

       serve("*.html").with(MyServlet.class);

     }

   }

There’s a varargs version:

 serve("*.html", "/my/*").with(MyServlet.class);

And a regex version:

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

Takes parameters:

Map<String, String> params = new HashMap<String, String>();

  params.put("coffee", "Espresso");

  params.put("site", "google.com");

  ...

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

MyServlet extends HttpServlet

MyServlet must be a @Singleton

http://code.google.com/p/google-guice/wiki/ServletModule

Available injections:

@RequestScoped

class SomeNonServletPojo {

  @Inject

  SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

    ...

  }

}

@Inject @RequestParameters Map<String, String[]> params;

The injector itself:

@Singleton

public class MyServlet extends HttpServlet {

  @Inject private Injector injector;

  ...

}

http://code.google.com/p/google-guice/wiki/GoogleAppEngine

Ensure the AOP alliance, Guice, and Guice servlet jars are in the WEB-INF/lib directory of your .war file (or www directory):

  www/

      WEB-INF/

          lib/

              aopalliance.jar

              guice-servlet-snapshot.jar

              guice-snapshot.jar

              ...

          classes/

              ...

          appengine-web.xml

          web.xml

Make sure you have the right jars