我想在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
。所以为什么?以及如何解决它?
答案 0 :(得分:0)
方法getThreadLocalRequest()
在类AbstractRemoteServiceServlet
中实现,该类由RemoteServiceServlet
继承。该实现仅为方法perThreadRequest
设置字段getThreadLocalRequest()
(由doPost()
使用)。这意味着,getThreadLocalRequest()
只会在POST请求中调用请求时返回请求,并且只有在没有调用super方法的情况下没有覆盖doPost()
时才会返回。
所以要确保getThreadLocalRequest()
不会返回null
doPost()
super.doPost()