This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to inject a resource using custom annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Servlets
Bookmark "how to inject a resource using custom annotation" Watch "how to inject a resource using custom annotation" New topic

how to inject a resource using custom annotation

shwetank singh

Joined: Apr 02, 2007
Posts: 26
Hello Ranchers!

Please help understand how can i use a custom annotation to inject ServletRequest into an object.
is this understanding i have correct? :
a) only resources managed by container can be derived or injected
b) if i define an annotation, i should have the annotation processor for it
c) i need something like:

where myAnnotation should be able to inject ServletRequest into myRequest

1) is my understanding correct?
2) if it is, how to achieve this, how do i wire it together?
3) how is annotation state maintained?
4) can i guarantee each object will receive its own copy from container, with its association to servlet request?

please move the post to correct module, i can't see something dedicated to annotations.

help! : )
Rob Spoor

Joined: Oct 27, 2005
Posts: 20078

As far as I know, the only way to wrap ServletRequest (and ServletResponse) is to perform the wrapping inside a Filter. You there create an instance of a ServetRequestWrapper / HttpServletRequestWrapper sub class and pass that as the first argument to the FilterChain's doFilter method:
You then need to make sure this filter's called for each request you want it to be called for; applying it in the web.xml file for url-pattern /* should take care of that.

I'll move this thread to our Servlets forum, as I think that's a better location for it.

How To Ask Questions How To Answer Questions
shwetank singh

Joined: Apr 02, 2007
Posts: 26
Thanks Rob!
Yes i agree we can use a filter and then use the cascaded request object. The problem is, the architecture i am dealing with for this problem won't hand over the object passed this way (or any other manner).
i am specifically looking to implement injection based mechanisms for this. i think this request object can be derived from container and added to a state which can then be injected anywhere i wish to.
the problem is i am unable to figure that out : )
Much thanks for help!
I agree. Here's the link:
subject: how to inject a resource using custom annotation
It's not a secret anymore!