在java中通过web服务维护多个会话请求

发布于 2024-12-02 02:49:48 字数 627 浏览 1 评论 0原文

我想使用 Java Web Services

在此处输入图像描述 实现以下解决方案(在图像中描述) 当用户使用 Web 服务请求有效凭证时,就会在服务器上创建一个会话,并且该服务器(接收请求的服务器)会创建与其他服务器(即 Meta Trader 的服务器)的连接。

这里每个用户都有一个不同的会话来维护他们与元交易服务器的连接和状态。

注意: 目前,当用户请求时,我不维护任何会话,而是将连接对象保存在 a 中

  @javax.ws.rs.core.Context
  ServletContext servletContext;

  MyApplication application = new MyApplication();
  servletContext.setAttribute("application", application);

,但此解决方案自然不能为多个用户提供服务。因此,请任何人知道如何解决为多个客户提供服务的问题,然后请回复。

我正在使用 Glassfish 和 JAX-RS (Jersery 1.1)、JAXB

I want to implement following solution (described in a image) using Java Web Services

enter image description here
When ever a user request with a valid credentials using web services , a session is created over server and that server (who receives the request ) creates a connection with the other server i.e. Meta Trader's Server.

Here each user has a different session to maintain their connection and a state with meta trader server.

Note:
Currently i am not maintaining any session when a user request instead i am saving the connection object in a

  @javax.ws.rs.core.Context
  ServletContext servletContext;

  MyApplication application = new MyApplication();
  servletContext.setAttribute("application", application);

But this solution doesn't serve multiple users naturally. so please anyone has an idea how to solve the issue of serving multiple clients then please reply.

I am using Glassfish and JAX-RS ( Jersery 1.1 ) , JAXB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-12-09 02:49:48

只需使用注释 @javax.ws.rs.core.Context 即可获取 HttpServletRequest 并在部署 Jersey 的容器中使用其会话。

下面的代码是球衣资源的简单示例,它获取会话对象并将值存储在会话中,并在后续调用中检索它们。

@Path("/helloworld")
public class HelloWorld {

    @GET
    @Produces("text/plain")
    public String hello(@Context HttpServletRequest req) {

        HttpSession session= req.getSession(true);
        Object foo = session.getAttribute("foo");
        if (foo!=null) {
            System.out.println(foo.toString());
        } else {
            foo = "bar";
            session.setAttribute("foo", "bar");
        }
        return foo.toString();


    }
}

但您不应该像这样使用 RESTful API。它意味着用作无状态的 Web 服务,而不是 Web 应用程序。检查以下答案,我从中得到了示例和建议

(球衣安全和会话管理)
https://stackoverflow.com/a/922058
https://stackoverflow.com/a/7752250

(如何管理 JAX-RS 中的状态?)
https://stackoverflow.com/a/36713305

(在 JAX-RS 资源中获取 ServletContext)
https://stackoverflow.com/a/1814788

Simply use the annotation @javax.ws.rs.core.Context to get the HttpServletRequest and use its session within the container in which Jersey is deployed.

The code below is a simple example of a jersey resource that gets the session object and stores values in the session and retrieves them on subsequent calls.

@Path("/helloworld")
public class HelloWorld {

    @GET
    @Produces("text/plain")
    public String hello(@Context HttpServletRequest req) {

        HttpSession session= req.getSession(true);
        Object foo = session.getAttribute("foo");
        if (foo!=null) {
            System.out.println(foo.toString());
        } else {
            foo = "bar";
            session.setAttribute("foo", "bar");
        }
        return foo.toString();


    }
}

But you should NOT use RESTful API like this. It meant to be used as web service which is stateless, not web application. Check the following answers which I got the example and advice from

(jersey security and session management)
https://stackoverflow.com/a/922058
https://stackoverflow.com/a/7752250

(How to manage state in JAX-RS?)
https://stackoverflow.com/a/36713305

(Get ServletContext in JAX-RS resource)
https://stackoverflow.com/a/1814788

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文