gpt4 book ai didi

gwt - HttpServletRequest 请求 = this.getThreadLocalRequest() 返回 null?

转载 作者:行者123 更新时间:2023-12-03 17:19:57 26 4
gpt4 key购买 nike

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

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 .所以为什么?以及如何修复它?

最佳答案

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

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

  • 确保您在 POST 请求中调用它(而不是 GET 或其他一些 HTTP 请求类型)。
  • 确保您没有覆盖 doPost()无需调用 super.doPost()
  • 关于gwt - HttpServletRequest 请求 = this.getThreadLocalRequest() 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673645/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com