Dear All,
I really need this program to work as soon as possible and need urgent
help! Mr. Christopher Schultz have given suggestions on how to solve the
problem and I've tried his suggestions. Thank you so much, as half of the
problem have been solved. I've created 3 files as suggested:
SaveFileApplet.java, SaveFileApplet.html and SaveFileServlet.java. This
solves the problem of the creation of a null file by the system. Now, a
file name that was input by the user was created BUT the problem is that,
when I open the file, the word "null" was written to the file NOT the file
content. The filename parameter was sent from the applet to the servlet
BUT the file content parameter was not sent. This means that the file
content parameter does not exist and the servlet did not receive the file
content parameter! Did I pass the file content correctly from
SaveFileApplet.java and did the SaveFileServlet receive the parameter
using req.getParameter("teditor");? I'm attaching the 3 files
(errer-free) as I mentioned. I really really hope that anyone can help me
solve this problem. Thank you so much.
FIRST FILE: SaveFileApplet.java
// C:\jakarta-tomcat-4.1.31\webapps\ROOT\SaveFileApplet.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
import javax.swing.text.BadLocationException;
import java.net.*;
import java.io.*;
import java.util.*;
public class SaveFileApplet extends JApplet implements ItemListener,
ActionListener
{
JPanel panel1, panellabel, panelbutton, paneltext;
JTextField namefile;
JButton jbtSave;
JTextArea textEditor;
JLabel labelfile;
public void init()
{
Container container = getContentPane();
namefile = new JTextField(10);
panel1 = new JPanel();
panellabel = new JPanel();
panelbutton = new JPanel();
paneltext = new JPanel();
labelfile = new JLabel("File Name");
panellabel.setLayout(new FlowLayout(FlowLayout.LEFT,50,0));
panellabel.add(labelfile);
panellabel.add(namefile);
panelbutton.setLayout(new GridLayout(1,1));
panelbutton.add(jbtSave = new JButton("Save"));
textEditor = new JTextArea(18,63);
textEditor.setFont(new Font("monospaced",Font.PLAIN,12));
JScrollPane scrollPane1 = new JScrollPane(textEditor);
Linenumber linenumber1 = new Linenumber ( textEditor );
scrollPane1.setRowHeaderView(linenumber1);
paneltext.add(scrollPane1);
panel1.add(panellabel);
panel1.add(paneltext);
panel1.add(panelbutton);
container.add(panel1);
jbtSave.addActionListener (
new ActionListener() {
public void actionPerformed (ActionEvent en) {
savefile();
}
}
);
} // end init
public void actionPerformed(ActionEvent ae)
{
} // End action perform
public void itemStateChanged(ItemEvent ie)
{
} // End item state changed
public void savefile()
{
String filename = namefile.getText();
String teditor = textEditor.getText();
URL servletUrl = null;
URLConnection con;
String servletName =
http://localhost:8080/examples/servlet/SaveFileServlet;
try
{
servletUrl = new URL(servletName + "?filename=" + filename);
con = servletUrl.openConnection();
con.setDoOutput(true);
con.connect();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
out.writeUTF(teditor);
out.flush();
out.close();
DataInputStream in = new DataInputStream(con.getInputStream());
in.close();
}
catch(Exception e)
{
System.out.println("Exception caught..."+e);
}
} // end savefile
} // end class SaveFileApplet
SECOND FILE: SaveFile Applet.html
<html>
<head>
<title>save file</title>
</head>
<body BGCOLOR=WHITE>
<div align="center">
<table WIDTH="70%" align="center">
<tr align="center" bgcolor="#CCC66H"><td><< SAVE FILE >></td></tr>
<tr align="center">
<td>
<applet CODE="SaveFileApplet.class" WIDTH=500 HEIGHT=400>
</applet>
</td>
</tr>
</table>
</div>
</body>
</html>
THIRD FILE: SaveFileServlet.java
//
C:\jakarta-tomcat-4.1.31\webapps\examples\WEB-INF\classes\SaveFileServlet.java
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SaveFileServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException
{
String name = req.getParameter("filename");
String content = req.getParameter("teditor");
String nameFile = "C:/temp/"+name;
FileWriter resultsFile = new FileWriter(nameFile,true);
PrintWriter toFile = new PrintWriter(resultsFile,true);
toFile.println(content);
toFile.close();
}
}
Yours Sincerely,
TEH NORANIS
Teh Noranis Mohd Aris <[EMAIL PROTECTED]> wrote:
Dear All,
Thank you so much for the explanation, Mr. Christopher Schultz. I
understand that by just using a "file" input type, I can upload a file
directly. In fact, I already construct the program and it is already
working. However, I choose to use applet to get the advantage of Java GUI
programming, which is event-driven. Actually, the save file module is only
part of the system. I'm going to try what has been suggested and I will
get to you soon. Thank you so much.
Yours Sincerely,
TEH NORANIS
Christopher Schultz wrote:
-----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 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]
---------------------------------
Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives. Check it out.
---------------------------------
TV dinner still cooling?
Check out "Tonight's Picks" on Yahoo! TV.