3

当我调用getThreadLocalRequest()servlet 中的方法(扩展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);
    }       
});
4

2 回答 2

2

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

}


.........

}
于 2013-04-08T06:33:49.057 回答
0

不要忘记在 web.xml 中添加以下配置以维护 HttpSession,否则会出现 IllegalStateException:

<listener>  
  <listener-class>  
    org.springframework.web.context.request.RequestContextListener  
  </listener-class>  
</listener>
于 2016-06-01T13:07:02.613 回答