views:

131

answers:

3

I have a J2ee application where I basically want two objects, created by two separate servlets to communicate directly and I need these intances to be stable, i.e. to "know" each other during the session.

The sequence is roughly:

  1. Client sends a request to Servlet #1, which creates object A
  2. Client sends a second request (after the first returns) to servlet #2 which creates object B.
  3. Object B finds A, using JNDI, and the two objects interact.
  4. The client now continues to send requests to object A which needs to find B again.

How do I make sure that these two instances know each throughout the session? Binding them to JNDI doesn't entirely solve the problem, since object B needs to communicate with its original servlet (servlet #2), which is not kept stable across requests.

Any ideas?

Thanks in advance.

A: 

To be honest: I don't fully understand what you are trying to achieve. Can you perhaps try to explain the problem you are trying to solve instead of the solution?

What do these objects depend on? Are they user specific? Then put them into the session and you can retrieve them from the session again (request.getSession().getAttribute("A")).

Are they global for all users? In that case put them into a spring configuration and retrieve them from there.

Never store any information inside the servlet.

EDIT: ok, so from what I understand storing the values in the session is imho the best way to solve this problem (in "Java-Pseudo-Code"):

public class BusinessServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res)  {
  HttpSession session = req.getSession(true);
  BusinessCode business = session.getAttribute("A");
  if (business == null) {       
    business = new BusinessCode();
    session.setAttribute("A", business);
  }

  DebugObject debug = session.getAttribute("B");
  if (debug == null) {       
    debug = new DebugObject();
    session.setAttribute("B", debug);
  }

  business.doSomeWork(debug); 
  }
} 

public class DebugServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse res)  {
    HttpSession session = req.getSession(true);
    DebugObject debug = session.getAttribute("B");
    if (debug != null) {
      debug.printDebugMessages(res);
    }
  }
}

Does this help?

jiriki
A: 

Yes, I admit the problem description is a bit vague. But it's not a very simple application. Still, I will try to ask it better:

My end goal is to create a sort of a "semantic debugger" for my application that, as opposed to a java debugger which simply debugs the java statements.

The application debugged is basically a servlet. which my tool connects to. The tool maintains a connection to the application through another servlet which controls the debugging process. These two servlets need to communicate with each other constantly and directly.

My current thought is to set up a stateful session bean that will facilitate this communication (never done it, still struggling with setting it up).

But I would appreciate any thoughts on how to achieve this better.

Lior
A: 

And what stops you from using the Session? You don't need JNDI, just place your object into session under a predefined name. If the communication object is application-wide, use Singleton.

P.S. It looks to me you're doing something weird, while the solution could in fact be simpler. Can you describe the task, not the proposed implementation? What is a "semantic debugger" anyway?

Vladimir Dyuzhev