Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get HttpServletRequestObject in the @service class

I'm successfully able to integrate Spring and Spring4GWT. Everything is working fine.

The only problem I'm facing is How do I get HttpServletRequestObject in the @service class?

Some of the configuration and code

web.xml

<servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/ui/test/*</url-pattern>
    </servlet-mapping>

Service class

public class LoginServiceImpl implements ILoginService {

    private IUserService userService;
    public LoginServiceImpl(IUserService userService) {
        super();
        this.userService = userService;
    }

    public boolean isAuthenticUser(String userName, String password) {
        // operation
    }
}

In LoginServiceImpl I'm not able to get the Servlet object. I need it here so that I can use it for different purposes.

Any Idea?

like image 987
user783789 Avatar asked Dec 08 '25 02:12

user783789


1 Answers

Finally I got a solution for it. If someone wants to have access of the HttpServletrequest in the GWT-RPC service then the following can help.

Modify 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>

In Service

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

Hope this is helpful

like image 91
user783789 Avatar answered Dec 12 '25 09:12

user783789