views:

33

answers:

4

Hello!

I'm trying create a new file with a Java Applet, but I don't know how send this file to the response output of the browser, such as any tipical webpage.

With a Servlet it is easy with "HttpServletResponse response", but is this possible with a applet?

I'm trying do this without sign the applet or use any servlet.

Thanks a lot!.

A: 

An applet is basically just a JAR file which you put on your web server and then you add a JNLP description so the browser knows what to do. Think of it as a complex HTML page because it doesn't go into WEB-INF but besides the other files for the browser (HTML, external JavaScript, images, CSS, ...)

This article from Oracle describes the steps.

Aaron Digulla
A: 

An applet can request resources from the web server it came from, e.g. images - Applet.getImage() or fetch other files like so:

URL url = new URL("myfile.txt");
URLConnection uc = url.openConnection();
InputStream in = new BufferedInputStream(uc.getInputStream());

int d;
while ((c = in.read()) != -1) {
  // do something with d (remember to cast to byte!)
}
andrewmu
+1  A: 

Don't use an applet for this. Go with the Servlet.

Why do you want to do this inside the applet? It will never be able to write anything to disk if you don't sign it, and it can only communicate with the browser through some Javascript API, not send a file directly. You can combine the functionality in your applet perfectly with Servlets, and direct the browser to any relevant page:

AppletContext a = getAppletContext();
URL url = new URL(link_to_your_servlet);
a.showDocument(url,"_blank");

That will open a new window in the browser, and download the file.

Matthijs Bierman
thanks for your answer. I will consider it. ;)
Jesús Galindo
A: 

You may, or may not, want to use LiveConnect to modify the DOM of the current page. It depends what you are generating. Typically you would want to generate on the server.

Tom Hawtin - tackline