Majid Laissi
Majid Laissi

Reputation: 19788

null pointer exception from Session

I'm using spring4gwt in my project.

I have the following login service implementation:

@Service("loginService")
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public UserBean checkUser(String userName, String password) throws Exception {

        HttpSession httpSession = getThreadLocalRequest().getSession();

    }
}

When I call the loginService.checkUser("test","test") (In hosted mode), I get a NullPointerException, as getThreadLocalRequest() returns NULL instead of the actual session.

I didn't try in web mode yet.

Why would I get a null session? Does it have something to do with spring4gwt?

Upvotes: 2

Views: 1814

Answers (1)

Majid Laissi
Majid Laissi

Reputation: 19788

Yes it is because of spring4gwt, we need a different approch.

I found the solution here http://code.google.com/p/spring4gwt/issues/detail?id=2:

in 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-url/*</url-pattern>
</filter-mapping>

and instead of :

 HttpSession httpSession = getThreadLocalRequest().getSession();

Use :

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession httpSession = attr.getRequest().getSession();

Upvotes: 6

Related Questions