How to get session object while working on webservices?

谁都会走 提交于 2020-01-02 13:11:27

问题


How to get session object while working on webservices?

Services are called between two programs. How to get user session object while workin with webservices. It is not possible to get session using request object as there will not be request or response when we talk about services.


回答1:


If you're working with JAX-WS to create your web services, then you can access the HttpServletRequest object (and hence your HttpSession object) via the WebServiceContext.

@WebService(...)
public class MyService {
    @Resource
    private WebServiceContext ctx;

    private HttpSession getSession() {
        HttpServletRequest req = (HttpServletRequest) this.ctx.getMessageContext()
                .get(MessageContext.SERVLET_REQUEST);
        return req.getSession();
    }
}

For a more extensive example, see, e.g., "Maintaining sessions using JAX-WS 2.0" by Art Frechette.



来源:https://stackoverflow.com/questions/2076613/how-to-get-session-object-while-working-on-webservices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!