0

对于某些背景,我将 JBoss AS 7 与 EJB 一起使用。当客户端最初连接以检索其会话 ID 时,我使用 erra 消息总线从客户端向我的服务器发送一条消息,以便稍后我可以从它发出请求并让服务器响应特定客户端。

我该怎么做呢?我可以以某种方式注入 HttpSession 对象服务器端吗?我对此很陌生,所以请多多包涵。如果我太含糊,请告诉我,我会尝试详细说明。

4

2 回答 2

0
// the following variable is in the Http servlet service() method arguments
// only shown here this way to demonstrate the process
javax.servlet.http.HttpServletRequest serviceRequest;
javax.servlet.http.HttpServletResponse serviceResp; // addCookie()
javax.servlet.http.HttpSession cecil;
javax.servlet.http.Cookie[] reqCk;


// "(boolean overload) "true" creates the session" or call the other overload version method with no argument 
// to retrieve the session getSession() "the server container stores and creates sessions"
// false in that version is to avoid bothering for a session to cut down uneeded processing


cecil = serviceRequest.getSession();//creates a session if it does not have one


String httpSession_ID = cecil.getID();


if((reqCk = serviceRequest.getCookies()) == null){

// perhaps create a cookie here using "new class "
// cookiePiece = new javax.servlet.http.Cookie("COOKIENAME",....); ....YOU MUST LEARN THE COOKIE PARTS WRITING RULES FOR BROWSER COOKIES !!! ; ; ;
serviceResp.addCookie(cookiePiece); // now is on the servers array "reqCk"

}else{

// process the cookie here using javax.servlet.http.Cookie methods

}

存储和检索数据的其他方法是会话范围的 JSP 或 JSF bean

于 2013-07-29T03:27:59.490 回答
0

如果您向 ErraiBus 服务方法发送消息,您将拥有Message可用的对象。您可以从中检索会话并获取该会话的 ID,如下所示:

@Service
public class ClientHelloService implements MessageCallback {
  @Override
  public void callback(final Message message) {
    HttpSession session = message.getResource(
        HttpServletRequest.class, HttpServletRequest.class.getName()).getSession();
    System.out.println("Client said hello. Session ID: " + session.getId());
  }
}

如果您将消息发送到 Errai RPC 端点,您将无法轻松访问该消息。在这种情况下,您将不得不使用以下RpcContext.getSession()方法:

@Service
public class ClientHelloRpcServiceImpl implements ClientHelloRpcService {
  @Override
  public void hello() {
    HttpSession session = RpcContext.getHttpSession();
    System.out.println("Client said hello. Session ID: " + session.getId());
  }
}

它的工作方式简单但丑陋:RpcContext 类将包含 RPC 请求的 Message 对象存储在 ThreadLocal 中,它只是从中检索 HttpSession。

于 2013-07-30T15:42:50.207 回答