I've abandoned my earlier mission to create the applet communicate directly using the database, despite the fact that customers and web pages have stated it's possible. I'm now looking to get my applet to pass through information (String and boolean format) joined in textfields or shown by checkboxes, and provide this towards the servlet, which in turn stores it properly within the database. I have got the applet front-end - the GUI - built, and also the servlet - database connection also built. The only issue may be the outcomes of the 2, applet and servlet. Wouldso would one pass String data from an applet to some servlet?

Thanks, Frederick G.

First of all, you need to acknowledge that you could only talk to the server where your applet was downloaded from, which includes the main harbour number, unless of course you need to fool around with permissions, applet signing and all sorts of that malarky. This is not just an Applet restriction, exactly the same is applicable to Expensive and JavaScript (though within the situation of JavaScript you will find methods to obtain around it).

Using either the "getCodeBase()" or "getDocumentBase()" method in your Applet will enable you to get a URL from which you'll obtain the component parts needed to construct a brand new URL that enables you to call a servlet.

Thus, your Applet should be being offered in the same server that the servlet is located on.

e.g. in case your Applet is incorporated in the following page:


...this means you may make calls to the URL that begins with


...relatively easily.

This is a crude, untested, example showing how you can call a Servlet. This assumes this snippet of code has been known as from inside a clear case of Applet.

URL codeBase = getCodeBase();
URL servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "/myServlet");

// assumes protocol is http, could be https
HttpURLConnection conn = (HttpURLConnection)servletURL.openConnection();

PrintWriter out = new PrintWriter(conn.openOutputStream());
out.println("hello world");


Then inside your servlet, you will get the written text sent by overriding doPost() and reading through the input stream in the request (the same handling proven and just reads first type of input):

public void doPost(HttpServletRequest req, HttpServletResponse res) {

   BufferedReader reader = req.getReader();
   String line = reader.readLine();
   System.out.println("servlet received text: " + line);


Obviously, that's only one approach. You might take your inputs and increase your query string such as this (URLEncoding not proven):

String queryString = "inputa=" + view.getInputA() + "&inputb=" + view.getInputB();

and append that for your URL:

URL servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "/myServlet?" + queryString);

However, it appears not unheard of to develop some type of string and stream it towards the servlet rather nowadays.

A suggested format could be JSON as it is semi-structured, while being readable and you will find lots of (p)serializers around that will work inside your Applet as well as in your servlet. What this means is you'll have a nice object model for the data for you to share involving the Applet and Servlet. Accumulating a question string of complex inputs could be a mind bender.

Likewise, you can really use Java serialisation and stream binary for your Servlet which in turn uses Java serialisation to produce the right Java objects. However, should you stay with something similar to JSON, it'll mean your servlet is much more available to re-use since Java serialisation hasn't been implemented outdoors of Java (that i'm conscious of).

Hm, I suppose the applet and also the servlet run in 2 separate Java processes. For the reason that situation you will need to apply certain remoting technology, e.g. an http call to localhost. Actually, that's what servlets mostly are used and implemented for: Accept and process http demands.