2012-05-20 45 views
5

我想在GWT應用程序中獲得會話屬性。我得到會話這樣HttpServletRequest request = this.getThreadLocalRequest()return null?

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

this.getThreadLocalRequest()總是null

CODE:

客戶:

@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

你已經解決了嗎? – kozla13

回答

0

方法getThreadLocalRequest()在類AbstractRemoteServiceServlet中實現,該類由RemoteServiceServlet繼承。該實施僅對方法doPost()設置字段perThreadRequest(由getThreadLocalRequest()使用)。這意味着,getThreadLocalRequest()只會在POST請求中調用該請求時才返回請求,並且只有在未調用超級方法時才覆蓋doPost()

因此,要確保getThreadLocalRequest()沒有返回null

  • 確保你一個POST請求中調用itfrom(而不是GET或其他一些HTTP請求類型)。
  • 確保你沒有覆蓋doPost()無需調用super.doPost()