Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Spring\Struts2 HttpServletRequest injection?

Posted on 2008-06-15
8
Medium Priority
?
2,648 Views
Last Modified: 2013-11-24
I am planning on integrating my Struts2 application with Spring's IoC container. The only thing that is stopping me is that I can't seem to find out how to inject the HttpServletRequest, HttpServletResponse, and HttpSession into a bean with servlet or request scope.  Is this even possible?
0
Comment
Question by:leobaz2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21789941
Don't think you can, why do u need to?

0
 

Author Comment

by:leobaz2
ID: 21790225
Some of my beans are going to be adding cookies and looking at the request and response.  I don't want to have to pass the request and response to the beans as method parameters.  Here is what I was thinking of doing:

I'll have a request scoped bean called something like MyWebContext that has setters for the request and response.  I'll have a struts2 intercepter that will get the bean from the Spring IoC and then set the request and responses.  When the other beans need the request or reponse, I will use DI to get MyWebContext and call the appropriate getter. I think this is the best and easiest way to do it.
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 252 total points
ID: 21790254
Yes, this is possible.  Your Action class can implement the following interfaces:

org.apache.struts2.interceptor.SessionAware
org.apache.struts2.interceptor.ServletRequestAware
org.apache.struts2.interceptor.ServletResponseAware

Each of these interfaces contains a single setter method (which is called by the ServletConfigInterceptor) that serves as the entry point for the objects you want.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 21790261
They don't sound like request scope beans, how r they defined?
0
 

Assisted Solution

by:srikanthradix
srikanthradix earned 248 total points
ID: 21798471
http://www.ibm.com/developerworks/web/library/j-sr2.html
http://cwiki.apache.org/WW/understanding-interceptors.html Look at the bottom you will find how to write Custom Interceptors.
May be these links can help you.
0
 

Author Comment

by:leobaz2
ID: 21803833
I've written custom intercepters before.  I think my technique might work.  I will have a request scoped bean that my intercepter will put the request and response in for each http request.  Later in the processing of the request, my code can get that request scoped bean and get the request and response.  This will allow singleton scoped beans to use the request, response, and session without having these objects passed throw method parameters.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question