Revision: 1484
Author: [email protected]
Date: Tue Feb 1 16:51:39 2011
Log: Adding RequestScope example.
http://code.google.com/p/google-guice/source/detail?r=1484
Modified:
/wiki/ServletModule.wiki
=======================================
--- /wiki/ServletModule.wiki Tue Nov 10 16:30:28 2009
+++ /wiki/ServletModule.wiki Tue Feb 1 16:51:39 2011
@@ -72,7 +72,7 @@
class SomeNonServletPojo {
@Inject
- public SomeNonServletPojo(HttpServletRequest request,
HttpServletResponse response, HttpSession session) {
+ SomeNonServletPojo(HttpServletRequest request, HttpServletResponse
response, HttpSession session) {
...
}
@@ -127,3 +127,81 @@
}}}
This way you can map several URI patterns to the same servlet. A similar
syntax is also available for filter mappings.
+
+
+= Using !RequestScope =
+
+Each servlet configured with a `ServletModule` is executed within <a
href="http://code.google.com/p/google-guice/source/browse/trunk/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java#45">
RequestScope</a>.
+
+By default, there are several elements available to be injected, each of
which is bound in !RequestScope:
+ * `HttpServletRequest` / `ServletRequest`
+ * `HttpServletResponse` / `ServletResponse`
+ * {{{@RequestParameters Map<String, String[]>}}}
+
+Remember to use a `Provider` when injecting either of these elements if
the injection point is on a class that is created outside of
`RequestScope`. For example, a singleton servlet is created outside of a
request, and so it needs to call `Provider.get()` on any `RequestScoped`
dependency only after the request is received (typically it its `service()`
method.
+
+ The most common way to seed a value within `RequestScope` is to add a <a
href="http://download.oracle.com/docs/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/Filter.html">Filter</a>
in front of your servlet. This filter should seed the scope by adding the
value as a request attribute.
+
+For example, assume we want to scope the context path of each request as a
`String` so that objects involved in processing the request can have this
value injected.
+
+The `Filter` to do this might look like this:
+
+{{{
+ protected Filter createUserIdScopingFilter() {
+ return new Filter() {
+ @Override public void doFilter(
+ ServletRequest request, ServletResponse response, FilterChain
chain)
+ throws IOException, ServletException {
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ // ...you'd probably want more sanity checking here
+ Integer userId =
Integer.valueOf(httpRequest.getParameter("user-id"));
+ httpRequest.setAttribute(
+ Key.get(Integer.class, Names.named("user-id")).toString(),
+ userId);
+ chain.doFilter(request, response);
+ }
+
+ @Override public void init(FilterConfig filterConfig) throws
ServletException { }
+
+ @Override public void destroy() { }
+ };
+ }
+}}}
+
+And the binding might look like this:
+{{{
+ public class YourServletModule extends ServletModule {
+ @Override protected void configureServlets() {
+ .....
+ filter("/process-user").through(createUserIdScopingFilter());
+ }
+ }
+}}}
+
+And the servlet to use this might look like this:
+{{{
+@Singleton
+class UserProcessingServlet extends HttpServlet {
+
+ private final Provider<Integer> userIdProvider;
+ ....
+
+ @Inject UserProcessingServlet(
+ Provider<Integer> userIdProvider,
+ .....) {
+ this.userIdProvider = userIdProvider;
+ }
+
+ ....
+
+ @Override public void doGet(
+ HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ ...
+ Integer userId = userIdProvider.get();
+ }
+}
+}}}
+
+
+
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.