What would be the best way to match up two object instances between two different applications in a J2EE server?

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.

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.

Asked by: Lana155 | Posted: 21-01-2022

Answer 1

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?

Answered by: Kelvin518 | Posted: 22-02-2022

Answer 2

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);


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

Does this help?

Answered by: Tara130 | Posted: 22-02-2022

Similar questions

java - Passing class instances between applications in Android

I have an application App1 which defines class A and uses instances of this class. What I want to achieve is - after App1 is installed on the device to be able to load App2 which defines and implements class B which is subclass of class A (imported from App1 package); and be able to get an instance of class B in the App1. Class B doesn't add any new interfaces, so using it as class A is ok. But I am not sure how to pass th...

java - How to share session across two web applications deployed in two different tomcat instances

I have two different web applications running on two different instances of tomcat. I need to be having a common page with links to both the applications. But I should be able to log in only once and should be able to access links in the other application without being asked to login again. Is there a way to do it ?

java - debug two applications in tomcat in different eclipse instances

I have two server java applications. One of the applications is running in one eclipse instance - it connected to remote tomcat application to port 5005. Another application is running in another eclipse instances. But when I try to connect it to remote application to port 5005 - I've got connection refused error. I start tomcat after...

java - Multiple jetty instances for different applications

I am new to applications deployments in web servers altogether. Is it OK to add different instances of jetty webserver for two application - One data service and another angular UI application. Or Do I need to deploy the two applications from the same jetty instance.

java - What is a good toolkit for developing Blackberry applications?

Looking for a toolkit/SDK for general Blackberry development or application toolkits. Anything like an MVC framework?

web applications - Abstract Java Grid

I'm looking for an abstract representation of a grid in Java (grid as in that thing with columns and rows of data). Do such things exist? I want to be able to sort, filter, keep track of rows, set column properties etc. I'll then be realising the grid on the web. To be clear: I've already decided on the ultimate view technology (client side). The thing I'm looking for will hold all the records and offer them up...

apache flex - Client Java vs (Adobe) Flash for web applications, what to choose and when

A few years ago client Java was unsuitable for web development because a remarkable part of web users did not have Java installed. ( I don't remember exact numbers, more than 10%). Now I see the Google Analytics stats for a big site and it tells that >98% of users have Java installed. Is these stats very biased by Javascript usage? As I understand Google Analytics measure only users that has Javascript.

web applications - Is there a tool to generate web pages based on Java beans?

Is there a tool that will take you java beans (pojos) and automatically make them a form for a webpage? To be more clear I have a bunch of Jaxb objects that I want to be able to display in a form without having to code a bunch of html. Is there a tool that will read the jaxb objects and generate the editable form for me? Also, this needs to run on a glassfish server...

What do I need to write Java-based web applications and test them on my personal computer?

I currently have Apache HTTP Server, but I'm guessing I'll need Tomcat (and then have to configure it in a way that makes it not open to the public), a Java JDK (which I already have, but should probably update), and an IDE (I have Eclipse). But what else should I have or know before starting?

java - How does IIS compare in terms of performance and scalability to weblogic and websphere for enterprise web applications?

Closed. This question is opinion-based. It is not c...

c# - IPC between .NET and Java client applications

I must get two different client application talk without any kind of broker or server. What is the best method for IPC beween two process, Java and .NET? It must able to be work in multi-user termainl server, so no socket please. I wish that it is lightweight and simple, something plug and run, so no RMI/WS please. I'm now thinking about JNI to access Mutex and Named pipes, am I going to a right direction?

Is Java suitable for "Web 2.0" applications?

Closed. This question is opinion-based. It is not c...

How to develop multi-touch applications in Java?

Anticipating the day when multi-touch interfaces become more pervasive, are there libraries in Java that can be used for developing touch applications? I'm looking for interfaces similar to MouseListener / MouseMotionListener / MouseWheelListener.

web applications - What is a good embeddable Java LDAP server?

I'm working on a Java web application that integrates with a few other external applications that are deployed along with it. Authentication information must be synchronized across everything and the other applications want to authenticate against LDAP. The application will be deployed in environments where there will be no other LDAP server for everything to use; I have to provide it. My solution so far has been to use

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)