-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Teh,

Teh Noranis Mohd Aris wrote:
> In
> my program, I want the servlet to load an applet where I can input
> the file name in a given text field and the file content in a given
> text area.

Just out of curiosity, why do you want an applet to do this when you can
just use a "file" input type and upload a file directly?

> When the user has input the information, I want to POST
> the file name and its content from the applet to the servlet.

You are not using a POST. You are using a GET. If you want to use a
POST, you will have to write your file content to the request body,
instead of putting it into a URL parameter.

You had something like this:

String url = "http://.../servlet?filename=[name]&content=[contents]";;

URLConnection conn = new URL(url).openConnection();
conn.connect();
conn.close();

Instead, you should do something like this:

String url = "http://...servlet?filename=[name]";;
URLConnection conn = new URL(url).openConnection();
conn.setDoOutput(true);
conn.setHeader("Content-Length", contents.length());
conn.connect();

OutputStream out = conn.getOutputStream();
out.write(contents);
out.flush();
out.close();

conn.close();

This will put the contents of your file into the request body, instead
of in the URL parameters.

> I do not know whether I pass the
> parameters from the applet to the servlet correctly.

To check that the parameters are correct, put something like this into
your servlet code:

System.out.println("param[filename] = "
     + request.getParameter("filename"));

Also, you might want to print out the fill path of the file you are
trying to create. This really ought to do it:

File target = new File("C:\\TEMP", filename);

FileOutputStream out = new FileOutputStream(target);
out.write(contents);
out.flush();
out.close();

> After I input
> the file name and the file content, 2 files were created by the
> system: 1. a file with the name null 2. a source code file with a
> file name from the user input with no content at all.

I'm not sure how that extra file is being created. Your code only had a
single "open" call. Is it possible that "null" was created from an old
copy of the code, and you just forgot to delete the file?

Finally, don't use a FileWriter unless you know that you are dealing
with text. Otherwise, binary files will be corrupted. I'm not sure if
that matters to you, but creating a servlet that handles any type of
file will be more useful than one that only handles text.

> Yes, I'm using
> the same servlet to save a file and serving a page to save files.
> What is actually the matter with that approach?

It just seems that the "save file" servlet should do nothing but save a
file. Why have this servlet return any content at all? If I were writing
this "system", I would have the following files:

applet.html     (contains <object> element to serve the applet)
SaveFileServlet (saves files, as discussed)

When you want to display the applet, just display applet.html.
Otherwise, your SaveFileServlet will write to a file called "null" every
time you try to display the page (that's probably where your "null" file
is coming from).

Only call the SaveFileServlet when you actually want to save data.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF6b3n9CaO5/Lv0PARApg9AKCHsXSGif7v69laYl4u1DHmGvRrHACbB7cz
z2uU32oMdoJKcvL1/9EXf6A=
=sF79
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to