jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Basic concept in Applet-Servlet communication ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Basic concept in Applet-Servlet communication ?" Watch "Basic concept in Applet-Servlet communication ?" New topic

Basic concept in Applet-Servlet communication ?

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
I need to understand how a web-server applet identifies the target app-server servlet that it needs to communicate with when the user clicks on a link in IE or Netscape.
For example, in the web-server, I have a class file with these codes :

According to Zukowski's "JAVA 2 J2SE1.4" , pg 203, the getCodeBase() method will "...load the images relative to where the browser loaded the applet's class file..." which in my case will take you to /com/developer under the web-browser's Apache default /var/www/html
Does this mean I always have to have exactly the same directory tree in the web-server as I do in the app-server ???
At this point, how does the applet resolve the placeholder "servlet", it hasn't got the parameter names yet. To get the names, you need to identify the servlet class file first ???
I believe the getParameter() method will get the values of the parameters in the <applet...> as follows...

I believe by inserting <param name=\"MrSpock\" ... > the web-server will be able to set the value of servlet.
But how does the applet in the web-server identifies the servlet class file in order to get to MrSpock ???
I am stuck with this chicken-n-egg conundrum - most probably because I do not understand the basic concept of applet-servlet communication.
Please enlighten me ...

humanum errare est.
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

So let's break it down:
1) You can on a web page that, among other things, downloads an Applet class to run in the page. The Applet class, from the web server's perspective, is nothing more than a file; it happens to contain code, but the server maintains no understanding of how you will use that file.
2) Your JVM-enabled browser loads the Applet class, creates some graphical real estate on the browser page for it (or not, if it's a "background" applet), and the applet runs in the context of the browser page the called it.
3) It's the context of the browser page that matters. This includes the URL of the containing page, and by extension the URI of the applet's codebase on the webserver. So, when you go looking for stuff relative to the codebase, you intend to find it based on where the applet was. You could just as easily find it relative to getDocumentBase(), which is the directory the HTML page came from.
4) The context of this page also includes any parameters embedded in the HTML, including a URL at which to find the servlet. There's nothing yet that says the servlet must come from the same server; it can be a completely different URL.
5) So it's up to the HTML to provide a parameter of two that help the applet find the servlet, whereever it is, but this is a separate call back to some servlet-enabled server.
6) An applet calls up a servlet the same way you or I do: by sending a URI the server recognizes and maps to wherever servlets live. Nothing says this has to have any location or context relative to the applet's codebase or document base.
7) All your applet does, really, is automate the call to the servlet. What is does with servlet applet is up to the auspices and whims of the applet programmer.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Thanks, Michael.
I think I have been enlightened.

However I'll reconsider using URLConnection and rewrite the whole thing.
I agree. Here's the link: http://aspose.com/file-tools
subject: Basic concept in Applet-Servlet communication ?
jQuery in Action, 3rd edition