SeamFramework.orgCommunity Documentation
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.
Since the bean produced is dependent-scoped, use of the @RequestParam
annotation on class
fields and bean properties is only safe for request-scoped beans. Beans with wider scopes should wrap this
bean in an Instance
bean and retrieve the value within context of the thread in which
it's needed.
@Inject @RequestParam("id")
private Instance<String> bookIdResolver;
...
String bookId = bookIdResolver.get();
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.
Since the bean produced is dependent-scoped, use of the @HeaderParam
annotation on class
fields and bean properties is only safe for request-scoped beans. Beans with wider scopes should wrap this
bean in an Instance
bean and retrieve the value within context of the thread in which
it's needed.
@Inject @HeaderParam("User-Agent")
private Instance<String> userAgentResolver;
...
String userAgent = userAgentResolver.get();
The ServletContext
is made available as an application-scoped bean. It can be injected
safely 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 safely 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 safely 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
safely 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 safely 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 safely into any request-scoped CDI bean as follows:
@Inject @ContextPath
private String contextPath;
You can safely 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
safely 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
instances 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.
Since the bean produced is dependent-scoped, use of the @CookieParam
annotation on class
fields and bean properties is only safe for request-scoped beans. Beans with wider scopes should wrap this
bean in an Instance
bean and retrieve the value within context of the thread in which
it's needed.
@Inject @CookieParam("username")
private Instance<String> usernameResolver;
...
String username = usernameResolver.get();
The server info string is made available as a dependent-scoped bean. It can be injected safely into any CDI bean as follows:
@Inject @ServerInfo
private String serverInfo;
The context path is retrieved from the ServletContext
.