Archive - 06.10.2015

Request-scoped resources with Guice and Servlet

Our team was looking for a very simple Java Web framework for one of our projects, and we found the next combination of tools very fascinating:

  • Eclipse Jetty Web server for HTTP request interception
  • Google Guice for dependency injection
  • Java Servlet for request handling

They work together pretty well, and there is a plenty of articles on the Internet covering this topic, so it is easy to get things started. But none of these articles explains how to scope your code in a single HTTP request. By design, servlet is always a singleton. Guice refuses to register servlets as request-scoped objects, so, the whole point of request-scoped object instantiation is being lost. Due to this issue, we’ve almost decided to abandon an idea of using low-level servlets and switch to Jersey framework, which wasn’t very attractive for us as well, for different reasons. But a deeper look to Guice API has saved my day.

Read More