Dear All, I've already modified my program based on Mr. Christopher's solution but thers's still the word "null" in the file. You probably have a null content variable. String.valueOf(null) returns "null", so that's probably what's happening.
I only declared URL servletUrl = null but now I already commented it. For your information, the purpose of my program is ONLY to save information from the client to the server. I don't intend to send back information from the server to the client. My complete program looks like this now after the suggested modification (ClientIdea.java, ServerIdea.java, ClientIdea.html not included): // C:\jakarta-tomcat-4.1.31\webapps\ROOT\ClientIdea.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 ClientIdea 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; URL servletUrl; URLConnection con; String servletName = http://localhost:8080/examples/servlet/ServerIdea; try { servletUrl = new URL(servletName + "?filename=" + filename); con = servletUrl.openConnection(); con.setDoOutput(true); con.setRequestProperty("Content-Type","text/plain;charset=UTF-8"); con.connect(); OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(),"UTF-8"); out.write(teditor, 0, teditor.length()); 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 ClientIdea // C:\jakarta-tomcat-4.1.31\webapps\examples\WEB-INF\classes\ServletIdea.java import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServerIdea extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String name = req.getParameter("filename"); String content = req.getParameter("teditor"); PrintWriter outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream("C:/temp/"+name))); outFile.println(content); outFile.close(); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doGet(req, res); } } Did I miss out anything? Please help me! Thank you. Yours Sincerely, TEH NORANIS Christopher Schultz <[EMAIL PROTECTED]> wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Teh, Teh Noranis Mohd Aris wrote: > 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. You probably have a null content variable. String.valueOf(null) returns "null", so that's probably what's happening. > The filename > parameter was sent from the applet to the servlet BUT the file > content parameter was not sent. You're going to want to fix that. > This means that the file content > parameter does not exist and the servlet did not receive the file > content parameter! Sounds about right. If I were you, I would modify my program to do nothing if there's no file content parameter. (Well, I would actually use PUT and place the content in the body of the request, but...). > servletUrl = new URL(servletName + "?filename=" + filename); > con = servletUrl.openConnection(); > con.setDoOutput(true); > con.connect(); So far so good. > ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); > DataOutputStream out = new DataOutputStream(byteOut); > out.writeUTF(teditor); > out.flush(); > out.close(); Uhh... you put your data into a ByteArrayOutputStream and then did nothing with it at all. You need to write it to the URLConnection's OutputStream: If you are going to be using UTF, you need to specify that when connecting to your server so it knows what character set you are using. Use the "Content-Type" header for this: con.setRequestProperty("Content-Type", "text/plain; charset=UTF-8"); DataOutputStream uses "modified" UTF-8, and is actually intended to be used with Java object serialization. Don't do this. Instead, use real UTF-8 like this: Instead of the above, do this: OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "UTF-8")); out.write(teditor, 0, teditor.length()); out.flush(); out.close(); > DataInputStream in = new DataInputStream(con.getInputStream()); > in.close(); To be correct, you ought to empty this inputstream before closing it. Also, if you ever hope to use non-text data, you should change your content type to "application/octet-stream" with no encoding at all (the encoding is basically "raw"). You'll also want to write bytes instead of Strings to your output stream and get rid of the OutputStreamWriter. - -chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF9WgG9CaO5/Lv0PARAmeOAJ9HpfJPFQxkH2MTUnFn0qWs0MZ/PwCfY1Fj GvWkPIZAvF8TDKBzPTJ+p2E= =crVh -----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] --------------------------------- It's here! Your new message! Get new email alerts with the free Yahoo! Toolbar.