繁体   English   中英

JAX-WS,WebServiceContext和会话

[英]JAX-WS, WebServiceContext and Session

我可以在Web服务中使用WebServiceContext和方法getMessageContext()获取用于保存的HttpSession对象并获取会话值吗? 我试图在这样的Web服务中使用HttpSession:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class DummyWs {

    @Resource
    private WebServiceContext wsContext;

    @WebMethod(operationName = "sayHello")
    public String sayHello(@WebParam(name = "name") String name) {
        return "hello " + name;
    }

    @WebMethod(operationName="setValue")
    public void setValue(@WebParam(name = "value") String newValue) {
        MessageContext mc = wsContext.getMessageContext();
        HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        session.setAttribute("value", newValue);
    }

    @WebMethod(operationName="getValue")
    public String getValue() {
        MessageContext mc = wsContext.getMessageContext();
        HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        return (String)session.getValue("value");
    }

}

我看到了其他使用@Stateful注释的示例,但我没有使用它。 有必要使用@Stateful批注吗? 如果我不使用此注释会怎样?

您是否看到了参考实现 zip发行版中包含的stateful示例?

该示例在Web服务实现中使用注释com.sun.xml.ws.developer.Stateful和类com.sun.xml.ws.developer.StatefulWebServiceManager存储对象。 由于要求Java Web服务必须是无状态的,因此我们需要在客户端调用之间将对象的内容保存在持久性存储中。

换句话说,您应该更喜欢稳定的服务。 失败处理,与事务语义的交互很简单。 并且增强了可重用性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM