joshishimla99 / spring4gwt

Automatically exported from code.google.com/p/spring4gwt
Apache License 2.0
0 stars 0 forks source link

How do I get HttpServletRequestObject in the @service class #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. With spring4gwt things work like a charm, but dont know how
to get the HttpRequest  object, earlier I used to to getThreadLocaRequest();;
obHttpSession session = getThreadLocalRequest().getSession(false);

What is the expected output? What do you see instead?
Looking for HttpRequest

What version of the product are you using? On what operating system?
Latest on Windows
Please provide any additional information below.

Original issue reported on code.google.com by bmakar...@gmail.com on 1 Oct 2010 at 4:35

GoogleCodeExporter commented 9 years ago
I've also tried with 

ServletRequestAttributes sra=(ServletRequestAttributes) 
RequestContextHolder.currentRequestAttributes();

but doesn't work.

Are there in place any workaround??

Original comment by spiderj...@gmail.com on 27 Dec 2010 at 3:34

GoogleCodeExporter commented 9 years ago
May be possible i've found a possible workaround for this:

just declare in the web.xml of the application this listener (from spring 
distr.)

<listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

In the spring bean service just declare this:

@Autowired
private HttpServletRequest request;

And seems it work.
Done.

Original comment by spiderj...@gmail.com on 27 Dec 2010 at 4:05

GoogleCodeExporter commented 9 years ago
The above is not working for me.
Do we have any solution on this?

Original comment by ritesh.s...@chleon.com on 15 Jun 2011 at 6:01

GoogleCodeExporter commented 9 years ago
Mentioning the code which worked for me

Change your web.xml
-------------------

<filter>
    <filter-name>springRequestFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>springRequestFilter</filter-name>
    <url-pattern>/your_pattern/*</url-pattern>
</filter-mapping>

And in the ServiceImpl class, you can access the request attribute by using

ServletRequestAttributes sra = 
((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes());
sra.getRequest();

Hope it'll help

Original comment by ritesh.s...@chleon.com on 17 Jun 2011 at 4:49

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
@Paresh I think that your approach is not Thread Safe.

Original comment by vijaz...@gmail.com on 14 Sep 2011 at 7:22

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks Ritesh.Your suggested approach worked.

Change your web.xml
-------------------

<filter>
    <filter-name>springRequestFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>springRequestFilter</filter-name>
    <url-pattern>/your_pattern/*</url-pattern>
</filter-mapping>

And in your Service Impl class, add

@Autowired
private HttpServletRequest request;

And then you can access the http session as
request.getSession()or can set it like 
request.getSession().setAttribute(<variable>,<val> );

Vikram

Original comment by kohli.Vi...@gmail.com on 11 Mar 2012 at 5:26