Reputation: 51
I am always getting null when I call getThreadLocalRequest()
method in servlet (which extends RemoteServiceServlet
).
public class UPS_LpnListRPCServiceImpl
extends RemoteServiceServlet
implements IUPS_LpnRPCService {
@Override
public String getUserInfoFromHeader() {
LOGGER.debug(" getUserInfoFromHeader: ");
HttpServletRequest req = this.getThreadLocalRequest();
if (req != null) {
//HttpSession session = req.getSession();
remote_user = req.getHeader("ct-remote-user");
LOGGER.debug("req != null");
} else {
remote_user = "";
LOGGER.debug("req == null");
}
LOGGER.debug(" getUserInfoFromHeader: remote_user = " + remote_user);
return remote_user;
}
}
Calling in other place:
IUPS_LpnRPCServiceAsync service = GWT.create(IUPS_LpnRPCService.class);
service.getUserInfoFromHeader(new AsyncCallbackSupport<String>(false) {
@Override
public void onSuccess(String remote_user) {
GWT.log("getting remote call");
defaultMainScreen.setUsername(remote_user);
GWT.log("remote_user = " + remote_user);
}
});
Upvotes: 3
Views: 2472
Reputation: 1
Do not forget to add the following configuration to the web.xml in order to maintain the HttpSession, otherwise you will have and IllegalStateException:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Upvotes: 0
Reputation: 51
If you want to access the HttpServletRequest object you can use the below code snippet:
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class UPS_LpnListRPCServiceImpl extends RemoteServiceServlet implements
IUPS_LpnRPCService {
...........
...........
public String getUserInfoFromHeader() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
if (requestAttributes != null) {
HttpServletRequest req = requestAttributes.getRequest();
remote_user = req.getSession().getAttribute("j_username").toString();
}
}
.........
}
Upvotes: 1