简体   繁体   English

JAX-WS,WebServiceContext和会话

[英]JAX-WS, WebServiceContext and Session

Can I use WebServiceContext and the method getMessageContext() in a web service to get a HttpSession object for save and get a session value? 我可以在Web服务中使用WebServiceContext和方法getMessageContext()获取用于保存的HttpSession对象并获取会话值吗? I was trying use the HttpSession in a web service like this: 我试图在这样的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");
    }

}

I saw other examples that uses the @Stateful annotation, but I don't use this. 我看到了其他使用@Stateful注释的示例,但我没有使用它。 Is necessary use the @Stateful annotation? 有必要使用@Stateful批注吗? What happened if I don't use this annotation? 如果我不使用此注释会怎样?

Did you see the sample stateful included in the Reference Implementation zip distribution? 您是否看到了参考实现 zip发行版中包含的stateful示例?

The sample use the annotation com.sun.xml.ws.developer.Stateful in the web service implementation and the class com.sun.xml.ws.developer.StatefulWebServiceManager for store the objects. 该示例在Web服务实现中使用注释com.sun.xml.ws.developer.Stateful和类com.sun.xml.ws.developer.StatefulWebServiceManager存储对象。 As Java Web services are required to be stateless, we need to save the contents of the objects in a persistent storage across client calls. 由于要求Java Web服务必须是无状态的,因此我们需要在客户端调用之间将对象的内容保存在持久性存储中。

In another way, you should prefer a staless service. 换句话说,您应该更喜欢稳定的服务。 The failure-handling, the interaction with transactional semantics is simple. 失败处理,与事务语义的交互很简单。 And the reusability is enhanced. 并且增强了可重用性。

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

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