Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Spring\Struts2 HttpServletRequest injection?

Posted on 2008-06-15
8
Medium Priority
?
2,643 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

705 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