2

I am using GWT (2.5) with RPC, Spring and Postgresql for my project. My issue is about HttpSession handling . All queries to server are dispatched by Spring (DispatchServlet) to my GwtController (extends RemoteServiceServlet) . The particular RemoteService is injected in the GwtController . It is easy to get the HttpSession inside the GwtContorller. In example by getThreadLocalRequest().getSession() or just from request.getSession(). My question is how to get HttpSession object inside the RemoteService ?

public class GwtRpcController extends RemoteServiceServlet {
……………
private RemoteService remoteService;
private Class remoteServiceClass;
………………

public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    …………
} 

public String processCall(String payload) throws SerializationException {
    …………
}

public void setRemoteService(RemoteService remoteService) {
    …………….
}

}

My Interface - DataService which implements RemoteService

public class DataServiceImpl implements DataService {

public Data getData(){

    !!!!! Here I want to get HttpSession !!!!!
    …………………………

}

}
Devin Konny
  • 161
  • 1
  • 9

1 Answers1

3

You can maintain a ThreadLocal in your Servlet and store there your current Request, then expose your Request with a static method.

public class GwtRpcController extends RemoteServiceServlet {

  static ThreadLocal<HttpServletRequest> perThreadRequest = 
         new ThreadLocal<HttpServletRequest>();

  @Override
  public String processCall(String payload) throws SerializationException {
    try {
      perThreadRequest.set(getThreadLocalRequest());
      return super.processCall(payload);
    } finally {
      perThreadRequest.set(null);
    }
  }

  public static HttpServletRequest getRequest() {
    return perThreadRequest.get();
  }
}


 public class DataServiceImpl implements DataService {
    public Data getData(){
       HttpServletRequest request = GwtRpcController.getRequest();
       HttpSession session = request.getSession();
    }
 }
Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27