SeamFramework.orgCommunity Documentation

Chapter 23. Injectable Servlet objects and request state

23.1. @Inject @RequestParam
23.2. @Inject @HeaderParam
23.3. @Inject ServletContext
23.4. @Inject ServletRequest / HttpServletRequest
23.5. @Inject ServletResponse / HttpServletResponse
23.6. @Inject HttpSession
23.7. @Inject HttpSessionStatus
23.8. @Inject @ContextPath
23.9. @Inject List<Cookie>
23.10. @Inject @CookieParam
23.11. @Inject @ServerInfo
23.12. @Inject @Principal

Solder provides producers that expose a wide-range of information available in a Servlet environment (e.g., implicit objects such as ServletContext and HttpSession and state such as HTTP request parameters) as beans. You access this information by injecting the beans produced. This chapter documents the Servlet objects and request state that Solder exposes and how to inject them.

The @RequestParam qualifier allows you to inject an HTTP request parameter (i.e., URI query string or URL form encoded parameter).

Assume a request URL of /book.jsp?id=1.

@Inject @RequestParam("id")

private String bookId;

The value of the specified request parameter is retrieved using the method ServletRequest.getParameter(String). It is then produced as a dependent-scoped bean of type String qualified @RequestParam.

The name of the request parameter to lookup is either the value of the @RequestParam annotation or, if the annotation value is empty, the name of the injection point (e.g., the field name).

Here's the example from above modified so that the request parameter name is implied from the field name:

@Inject @RequestParam

private String id;

If the request parameter is not present, and the injection point is annotated with @DefaultValue, the value of the @DefaultValue annotation is returned instead.

Here's an example that provides a fall-back value:

@Inject @RequestParam @DefaultValue("25")

private String pageSize;

If the request parameter is not present, and the @DefaultValue annotation is not present, a null value is injected.

Similar to the @RequestParam, you can use the @HeaderParam qualifier to inject an HTTP header parameter. Here's an example of how you inject the user agent string of the client that issued the request:

@Inject @HeaderParam("User-Agent")

private String userAgent;

The @HeaderParam also supports a default value using the @DefaultValue annotation.

The ServletContext is made available as an application-scoped bean. It can be injected safetly into any CDI bean as follows:

@Inject

private ServletContext context;

The producer obtains a reference to the ServletContext by observing the @Initialized ServletContext event raised by this module's Servlet-to-CDI event bridge.

The ServletRequest is made available as a request-scoped bean. If the current request is an HTTP request, the produced bean is an HttpServletRequest. It can be injected safetly into any CDI bean as follows:

@Inject

private ServletRequest request;

or, for HTTP requests

@Inject

private HttpServletRequest httpRequest;

The producer obtains a reference to the ServletRequest by observing the @Initialized ServletRequest event raised by this module's Servlet-to-CDI event bridge.

The ServletResponse is made available as a request-scoped bean. If the current request is an HTTP request, the produced bean is an HttpServletResponse. It can be injected safetly into any CDI bean as follows:

@Inject

private ServletResponse reponse;

or, for HTTP requests

@Inject

private HttpServletResponse httpResponse;

The producer obtains a reference to the ServletResponse by observing the @Initialized ServletResponse event raised by this module's Servlet-to-CDI event bridge.

The HttpSession is made available as a request-scoped bean. It can be injected safetly into any CDI bean as follows:

@Inject

private HttpSession session;

Injecting the HttpSession will force the session to be created. The producer obtains a reference to the HttpSession by calling the getSession() on the HttpServletRequest. The reference to the HttpServletRequest is obtained by observing the @Initialized HttpServletRequest event raised by this module's Servlet-to-CDI event bridge.

If you merely want to know whether the HttpSession exists, you can instead inject the HttpSessionStatus bean that Solder provides.

The HttpSessionStatus is a request-scoped bean that provides access to the status of the HttpSession. It can be injected safetly into any CDI bean as follows:

@Inject

private HttpSessionStatus sessionStatus;

You can invoke the isActive() method to check if the session has been created, and the getSession() method to retrieve the HttpSession, which will be created if necessary.

if (!sessionStatus.isActive()) {

    System.out.println("Session does not exist. Creating it now.");
    HttpSession session = sessionStatus.get();
    assert session.isNew();
}

The context path is made available as a dependent-scoped bean. It can be injected safetly into any request-scoped CDI bean as follows:

@Inject @ContextPath

private String contextPath;

You can safetly inject the context path into a bean with a wider scope using an instance provider:

@Inject @ContextPath

private Instance<String> contextPathProvider;
...
String contextPath = contextPathProvider.get();

The context path is retrieved from the HttpServletRequest.

The list of Cookie objects is made available as a request-scoped bean. It can be injected safetly into any CDI bean as follows:

@Inject

private List<Cookie> cookies;

The producer uses a reference to the request-scoped HttpServletRequest bean to retrieve the Cookie intances by calling getCookie().

Similar to the @RequestParam, you can use the @CookieParam qualifier to inject an HTTP header parameter. Here's an example of how you inject the username of the last logged in user (assuming you have previously stored it in a cookie):

@Inject @CookieParam

private String username;

If the type at the injection point is Cookie, the Cookie object will be injected instead of the value.

@Inject @CookieParam

private Cookie username;

The @CookieParam also support a default value using the @DefaultValue annotation.

The server info string is made available as a dependent-scoped bean. It can be injected safetly into any CDI bean as follows:

@Inject @ServerInfo

private String serverInfo;

The context path is retrieved from the ServletContext.

The security Principal for the current user is made available by CDI as an injectable resource (not provided by Solder). It can be injected safetly into any CDI bean as follows:

@Inject

private Principal principal;