当我在servlet中调用getThreadLocalRequest()
方法(扩展RemoteServiceServlet
)时,我总是变为null。
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;
}
}
在其他地方打电话:
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);
}
});
答案 0 :(得分:1)
如果要访问HttpServletRequest对象,可以使用以下代码段:
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();
}
}
.........
}
答案 1 :(得分:0)
不要忘记将以下配置添加到web.xml以维护HttpSession,否则您将拥有和IllegalStateException:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>