【发布时间】:2013-11-15 03:46:40
【问题描述】:
我可以在 Web 服务中使用 WebServiceContext 和方法 getMessageContext() 来获取 HttpSession 对象以进行保存并获取会话值吗?我试图在这样的网络服务中使用 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 注解吗?如果我不使用这个注解会发生什么?
【问题讨论】:
-
不确定@Stateful 注解是否真的在做任何事情以使底层传输将会话信息添加到 HTTP 会话,但始终建议使用注解。另一方面,你真的需要使用有状态的网络服务吗?它通常总是建议尽可能使用无状态 Web 服务。你可能也想看看这个:stackoverflow.com/questions/988819/stateful-webservice
标签: java web-services