I am using a helper class in a servlet to remove some code from the servlet itself. I am injecting this helper class in the servlet with the CDI @Inject and this is also marked as @RequestScoped bean. Since this helper class is used to remove some code from the servlet I need access to the httprequest, response and session in this class. Is there a way to make these available via injection? I tried to use @inject on a field of type HttpServletRequest but I get an error from WELD.
Asked
Active
Viewed 385 times
3
-
The best way would be to switch to Java EE 7. If you can't do that, take a look at these alternatives: http://stackoverflow.com/questions/18189337/inject-httpservletrequest-in-cdi-sessionscoped-bean and http://stackoverflow.com/questions/13419887/injection-of-httpservletrequest – helderdarocha Mar 12 '14 at 01:37
-
@helderdarocha I am using Glassfish 4.0...so I should be using Java EE 7. – jimny Mar 12 '14 at 10:42
-
If the helper class is stateless consider marking it @ApplicationScoped. – Adrian Mitev Mar 13 '14 at 09:46
1 Answers
2
There are a couple of alternative solutions to this.
You could pass the
HttpServletRequest
to the helper. I mean instead of the helper being:@Inject HttpServletRequest request; public Xxx doSomeHelperWork() { // use request }
Make it:
public Xxx doSomeHelperWork(HttpServletRequest request) { // use request }
Use the DeltaSpike servlet module which can handle the injection of
HttpServletRequest
.

Nikos Paraskevopoulos
- 39,514
- 12
- 85
- 90
-
Thanks for your comment. I was already using Google Guice for this however I would like to switch to CDI and Java EE. It seems something so simple that it must be possible with CDI – jimny Mar 12 '14 at 12:44