5

我想在 GWT 应用程序中获取会话属性。我得到这样的会话

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。所以为什么?以及如何解决?

4

1 回答 1

1

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

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

  • 确保您在 POST 请求中调用它(而不是 GET 或其他 HTTP 请求类型)。
  • 确保你没有在doPost()没有调用的情况下覆盖super.doPost()
于 2015-08-20T22:16:09.457 回答