gpt4 book ai didi

java - 使用 Java Web 服务时如何访问 HttpServletRequest 对象

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:08 25 4
gpt4 key购买 nike

我正在使用 Java 6、Tomcat 6 和 Metro。我使用 WebService 和 WebMethod 注释来公开我的 Web 服务。我想获取有关该请求的信息。我尝试了以下代码,但 wsCtxt 始终为空。我必须采取什么步骤才能为 WebServiceContext 获取 null。

换句话说:我如何执行以下行来为 wsCtxt 获取非空值?

MessageContext msgCtxt = wsCtxt.getMessageContext();

@WebService
public class MyService{

@Resource
WebServiceContext wsCtxt;

@WebMethod
public void myWebMethod(){
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIP = req.getRemoteAddr();
}

最佳答案

我建议您将变量从 wsCtxt 重命名为 wsContext,或者将名称属性分配给 @Resource 注释。 J2ee tutorial on @Resource表示变量的名称用作查找的一部分。我在注入(inject)不同类型资源的 Glassfish 中使用资源注入(inject)时遇到了同样的问题。

尽管您的正确名称可能不是 wsContext。我正在关注这个 java tip .如果你喜欢变量名wsCtxt,那么在变量声明中使用name属性:

@Resource(name="wsContext") WebServiceContext wsCtxt;

关于java - 使用 Java Web 服务时如何访问 HttpServletRequest 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/133436/

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