I want to get a Attribute of session in GWT app. i get Session like this
HttpServletRequest request = this.getThreadLocalRequest();
System.out.println("Check HttpServletRequest");
HttpSession session = request.getSession();
but this.getThreadLocalRequest()
is always null
.
CODE :
Client:
@RemoteServiceRelativePath("service.s3gwt")
public interface getAttributeSession extends RemoteService
{
String getSessionAttribute(String name);
}
public interface getAttributeSessionAsync
{
void getSessionAttribute(String name, AsyncCallback<String> callback);
}
Server:
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 "";
}
}
When call method getSessionAttribute()
I see that this.getThreadLocalRequest()
always returns null
. So Why? And how to fix it?
The method getThreadLocalRequest()
is implemented in the class AbstractRemoteServiceServlet
, which is inherited by RemoteServiceServlet
. The implementation sets the field perThreadRequest
(which is used by getThreadLocalRequest()
) only for the method doPost()
. That means, getThreadLocalRequest()
will only return the request if it is called from within a POST request, and then only if you didn't overwrite doPost()
without calling the super method.
So to make sure getThreadLocalRequest()
doesn't return null
doPost()
without calling super.doPost()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With