What would be the best way to map two object instances between two different applications on a J2EE server?

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

The sequence is approximately:

  • The client sends a request to Servlet # 1, which creates an object A
  • The client sends a second request (after the first return) to servlet # 2, which creates object B.
  • Object B finds A using JNDI, and both objects interact.
  • Now the client continues to send requests to object A, which should again find B.

How can I make sure that these two instances are known by everyone throughout the session? Linking them to JNDI does not completely solve the problem, since object B must communicate with its original servlet (servlet # 2), which is not supported by stable requests.

Any ideas?

Thanks in advance.


Yes, I admit that the description of the problem is a bit vague. But this is not a very simple application. However, I will try to ask him better:

My ultimate goal is to create a “semantic debugger” for my application, which, unlike the java debugger, which simply debugs Java instructions.

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

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

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

0
source share
2 answers

And what prevents you from using the session? You don't need JNDI, just put your object in the session under a predefined name. If the communication object is system-wide, use Singleton.

PS It seems to me that you are doing something strange, while the decision can actually be simpler. Can you describe this task, not the proposed implementation? What is a semantic debugger?

+1
source

To be honest: I do not quite understand what you are trying to achieve. Can you try to explain the problem you are trying to solve, instead of solving it?

What do these objects depend on? Are they specific users? Then add them to the session and you can get them again from the session (request.getSession (). GetAttribute ("A")).

Are they global for all users? In this case, put them in the spring configuration and extract them from there.

Never store information inside a servlet.

EDIT: OK, so from what I understand, storing values ​​in a session is the best way to solve this problem (in the "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 it help?

0
source

All Articles