0

I have an applet that communicates with a servlet using Http (Not sockets). Currently, each instance of the applet (i.e. when each applet is run by a different client on a different computer), all the instances communicate with the same servlet. What I want is that each instance of the applet communicate with different instances of the same servlet. Is this possible?

mithun1538
  • 1,447
  • 8
  • 25
  • 32
  • 2
    The servlet container will most likely be using the same instance for all requests, or at the very least will not make it obvious if it is not. What is driving the requirement for different instances? – Rob Tanzola Apr 02 '10 at 12:09
  • I am trying to implement a chat server. I have a broadcast all code and I want to use that to broadcast only between 2 clients. Hence what I was thinking is that I create a new instance of that servlet containing the broadcast all code, and ask the two applets to use only that instance to simulate the "chat". That way groups of 2 clients will be using same instance of the servlet. – mithun1538 Apr 02 '10 at 12:28
  • The actual question is now at this link, in case anybody needs to understand the problem better. http://stackoverflow.com/questions/2567377/how-to-initiate-chatting-between-two-clients-and-two-clients-only-using-applets – mithun1538 Apr 02 '10 at 15:15

3 Answers3

2

You don't want to have different instances of the same servlet in webapp's lifetime. The normal practice is to use the HttpSession to distinguish between clients. You need to pass the HttpSession#getId() as parameter to the applet in question:

<param name="jsessionid" value="${pageContext.session.id}">

Then, in the Applet connect the Servlet as follows:

String jsessionid = getParameter("jsessionid");
URL servlet = new URL(getCodeBase(), "servleturl;jsessionid=" + jsessionid);
URLConnection connection = servlet.openConnection();
// ...

Here servleturl obviously should match servlet's url-pattern in web.xml. You can alternatively also set a Cookie request header using URLConnection.setRequestProperty().

Finally, in the Servlet, to get and store client specific data, do as follows:

// Store:
request.getSession().setAttribute("data", data);
// Get:
Data data = (Data) request.getSession().getAttribute("data");

Hope this helps.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, your answer is similar to that of rsp, but more detailed. This question is however a sub question to : The actual question is now at this link, in case anybody needs to understand the problem better. http://stackoverflow.com/questions/2567377/how-to-initiate-chatting-between-two-clients-and-two-clients-only-using-applets – mithun1538 Apr 02 '10 at 15:14
1

From your question it seems that your servlet contains state. Every applet will have a session with the servlet container which your servlet can access. You can create an object that holds the state per session and place that object as attribute in the session of the caller. This way the servlet container is free to share one servlet instance among many clients.

rsp
  • 23,135
  • 6
  • 55
  • 69
0

The usual way to handle instance-specific actions is to have information stored in the session scope made available by the servlet container, not by having information stored in the servlet itself.

For it to work, your applet must correctly send cookies or the JSESSIONID attribute as provided by the web container or the applet must request a instance specific URL inside the servlet.

I would suggest you familiarize yourself further with the Servlet API specification in order to learn more about what is available to you.

Also note that some application servers support the notion of "clients" which are programs invoked with code served from the application server which have direct access to the inside of the application server code. The actual communication is handled by libraries also provided by the applcation server so this is simple. Glassfish and Trifork can do this.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347