HttpServletRequest request = this.getThreadLocalRequest()返回null?

时间:2012-05-20 13:03:48

标签: gwt

我想在GWT app中获得会话属性。我得到像这样的会议

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

this.getThreadLocalRequest()始终为null

代码:

客户端:

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

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

服务器:

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 "";
    }
}

当调用方法getSessionAttribute()时,我看到this.getThreadLocalRequest()始终返回null。所以为什么?以及如何解决它?

1 个答案:

答案 0 :(得分:0)

方法getThreadLocalRequest()在类AbstractRemoteServiceServlet中实现,该类由RemoteServiceServlet继承。该实现仅为方法perThreadRequest设置字段getThreadLocalRequest()(由doPost()使用)。这意味着,getThreadLocalRequest()只会在POST请求中调用请求时返回请求,并且只有在没有调用super方法的情况下没有覆盖doPost()时才会返回。

所以要确保getThreadLocalRequest()不会返回null

  • 确保您在POST请求中调用它(而不是GET或其他HTTP请求类型)。
  • 确保在未调用doPost()
  • 的情况下未覆盖super.doPost()