gpt4 book ai didi

gwt - GWT 中 this.getThreadLocalRequest() 返回 null

转载 作者:行者123 更新时间:2023-12-02 12:20:08 25 4
gpt4 key购买 nike

当我调用servlet(扩展了RemoteServiceServlet)中的getThreadLocalRequest()方法时,我总是得到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);
}
});

最佳答案

如果您想访问 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();
}

}


.........

}

关于gwt - GWT 中 this.getThreadLocalRequest() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com