Java 正在尝试检索HttpSession对象

Java 正在尝试检索HttpSession对象,java,jakarta-ee,jboss,ejb,errai,Java,Jakarta Ee,Jboss,Ejb,Errai,作为一些背景,我将JBossAS7与EJB一起使用。当客户端最初连接以检索其会话ID时,我使用errai消息总线将消息从客户端发送到服务器,以便稍后我可以从客户端发出请求,并让服务器响应特定的客户端 我该怎么做呢?我可以在服务器端注入HttpSession对象吗?我对这个很陌生,所以请容忍我。如果我说得太含糊,请告诉我,我会尽量详细说明 // the following variable is in the Http servlet service() method arguments // o

作为一些背景,我将JBossAS7与EJB一起使用。当客户端最初连接以检索其会话ID时,我使用errai消息总线将消息从客户端发送到服务器,以便稍后我可以从客户端发出请求,并让服务器响应特定的客户端

我该怎么做呢?我可以在服务器端注入HttpSession对象吗?我对这个很陌生,所以请容忍我。如果我说得太含糊,请告诉我,我会尽量详细说明

// 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或JSFbean

如果您要向一个ErraiBus服务方法发送消息,那么
消息
对象将可用。您可以从中检索会话并获取会话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请求的消息对象存储在ThreadLocal中,它只是从中检索HttpSession。

切勿将Http组件与EJB组件混合使用,请注意EJB必须仅包含业务逻辑,并且从MVC模式的角度来看,HttpSession是控制器的一部分(因为您可以通过servlet或其他控制器访问它),而EJB是模型的一部分。与其在EJB中注入
HttpSession
,不如将必要的值作为参数传递到EJB中。@LuiggiMendoza我不知道该怎么做。当servlet启动时,我是否能够获取HttpSession请求,然后从中获取id?我可以在某个地方指定一些方法来执行您刚才提到的操作吗?您可以通过执行
HttpSession session=request.getSession(false)
对每个请求访问
HttpSession
,然后您可以使用
session.getId
获取与
HttpSession
关联的当前jssessionid。看起来如果您能解释一下您的功能需求以帮助您解决真正的问题会更好。@LuiggiMendoza我正在使用errai消息总线在我的服务器之间传输消息。当客户机最初连接到服务器以检索信息时,我希望获取其会话id以与errai总线一起使用,以便向特定客户机发送消息。主要的问题是,我在哪里可以得到这个请求对象?我如何在我的代码中的某个地方获得它?