HttpServletRequest request = this.getThreadLocalRequest () return null?

I want to get the session attribute in a GWT application. I get a session like this

HttpServletRequest request = this.getThreadLocalRequest();
System.out.println("Check HttpServletRequest");       
HttpSession session = request.getSession();

but this.getThreadLocalRequest()always null.

THE CODE:

Customer:

@RemoteServiceRelativePath("service.s3gwt")
public interface getAttributeSession extends RemoteService 
{
    String getSessionAttribute(String name);
}

public interface getAttributeSessionAsync 
{
    void getSessionAttribute(String name, AsyncCallback<String> callback);
}

Server:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import daTotNghiep.client.service.getAttributeSession;

public class getAttributeSessionImpl extends RemoteServiceServlet implements getAttributeSession 
{
    private static final long serialVersionUID = 1L;

    public String getSessionAttribute(String name) 
    {
    // TODO Auto-generated method stub

    HttpServletRequest request = this.getThreadLocalRequest();
    System.out.println("Check HttpServletRequest");
    if(request == null) System.out.println("SO BAD");

   //   HttpSession session = request.getSession();
   // System.out.println("ID session "+session.getId());

     return "";
    }
}

When the method is called getSessionAttribute(), I see that it this.getThreadLocalRequest()always returns null. So why? And how to fix it?

+5
source share
1 answer

The method getThreadLocalRequest()is implemented in a class AbstractRemoteServiceServletthat is inherited RemoteServiceServlet. The implementation sets the field perThreadRequest(which is used getThreadLocalRequest()) only for the method doPost(). This means that it getThreadLocalRequest()will return a request only if it is called from a POST request, and then only if you did not overwrite it doPost()without calling the super method.

, getThreadLocalRequest() null

  • , POST ( GET HTTP-).
  • , doPost() super.doPost()
0

All Articles