Return-Path: Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: (qmail 74850 invoked from network); 15 Feb 2007 09:01:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Feb 2007 09:01:15 -0000 Received: (qmail 77474 invoked by uid 500); 15 Feb 2007 09:01:10 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 77138 invoked by uid 500); 15 Feb 2007 09:01:09 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 77127 invoked by uid 99); 15 Feb 2007 09:01:09 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Feb 2007 01:01:09 -0800 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: local policy) Received: from [206.190.36.204] (HELO web53809.mail.yahoo.com) (206.190.36.204) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 15 Feb 2007 01:00:55 -0800 Received: (qmail 7424 invoked by uid 60001); 15 Feb 2007 09:00:33 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=x4zXp7ghWYT9bkkeEOmDY1OOGQr8h4O89NNKHWGzIPX/Bxq0YeomqB6F5KHB0EAUakDYvvA2C4YCQe8mopXFNsd8aCi/oaQaZXTKSYZzn2fksuThM72yYpRcjEUixy/qPYtphjVv5a+ppIlXQ/RWAPFHPOWsDmtrY60qvlqQX+w=; X-YMail-OSG: ljb0QTIVM1kQg_NQGHv.S9E6VFQrzIYYibOalQpntetaJIqw30lQTH.6d3bt0KfFbAF4.babTXVqAO5W1STb2Kr0BKh..WUAzQyG1D0enZSEPzZYh0JMS_sogBLgxwhfy1AJem_PTkoT.5Zm7cFQgSlBHA-- Received: from [202.185.32.254] by web53809.mail.yahoo.com via HTTP; Thu, 15 Feb 2007 01:00:33 PST Date: Thu, 15 Feb 2007 01:00:33 -0800 (PST) From: Teh Noranis Mohd Aris Subject: Cannot save file to Apache Tomcat server To: users@tomcat.apache.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-147936322-1171530033=:3485" Content-Transfer-Encoding: 8bit Message-ID: <480929.3485.qm@web53809.mail.yahoo.com> X-Virus-Checked: Checked by ClamAV on apache.org --0-147936322-1171530033=:3485 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Dear all, I really hope that someone can help me out. I have an error-free applet and servlet. When I type http://localhost:8080/examples/servlet/ServletIdea, an applet is loaded. From the applet, I type the file name in a text field and the file content in a text area. I want the file content to be saved in the specified file name. The problem is that the file cannot be saved to the server. I put ServletIdea.java in C:/jakarta-tomcat-4.1.31/webapps/examples/WEB-INF/classes. I put IdeaTool.java in C:/jakarta-tomcat-4.1.31/webapps/ROOT. The programs are as follows: //ServletIdea.java import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletIdea extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { HttpSession session = req.getSession(); PrintWriter out = res.getWriter(); out.println(""); out.println(""); out.println("Login"); out.println("

Servlet Idea

"); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); DataInputStream in = new DataInputStream(req.getInputStream()); PrintWriter out = res.getWriter(); String filename = in.readUTF(); String filecontent = in.readUTF(); if (filename != null || filecontent != null) { out.println("filename="+filename+"

Next"); out.println("filecontent="+filecontent); } else{ out.println("requestparams.no-params"); } out.println("

"); try { PrintWriter outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream("C://"+filename))); outFile.println(filecontent); outFile.close(); } catch (FileNotFoundException e){ out.println("Cannot open file "+filename); } catch (IOException e) { out.println("write data file IO exception"); } out.close(); } } // IdeaTool.java // Java core pakages import java.awt.*; import java.awt.event.*; // Java extension packages import javax.swing.*; import javax.swing.JApplet; import javax.swing.text.BadLocationException; import java.net.*; import java.io.*; import java.util.*; public class IdeaTool extends JApplet implements ItemListener, ActionListener { JPanel panel1, panellabel, panelbutton, paneltext; JTextField namefile; JButton jbtSave; JTextArea textarea1; JLabel labelfile; String line; String codeBase, servlet, sessionId; public void init() { codeBase = "" + getCodeBase(); servlet = getParameter("servlet"); if (servlet != null) { if (servlet.startsWith("/") && codeBase.endsWith("/")) { codeBase = codeBase.substring(0, codeBase.length() - 1); } } sessionId = getParameter("id"); Container container = getContentPane(); namefile = new JTextField(20); panel1 = new JPanel(); panellabel = new JPanel(); panelbutton = new JPanel(); paneltext = new JPanel(); labelfile = new JLabel("File Name"); panellabel.setLayout(new FlowLayout(FlowLayout.LEFT,90,0)); panellabel.add(labelfile); panellabel.add(namefile); panelbutton.setLayout(new GridLayout(1,1)); panelbutton.add(jbtSave = new JButton("Save")); jbtSave.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent en) { savefile(); } } ); textarea1 = new JTextArea(18,63); textarea1.setFont(new Font("monospaced",Font.PLAIN,12)); JScrollPane scrollPane1 = new JScrollPane(textarea1); Linenumber linenumber1 = new Linenumber ( textarea1 ); scrollPane1.setRowHeaderView(linenumber1); paneltext.add(scrollPane1); panel1.add(panelbutton); panel1.add(panellabel); panel1.add(paneltext); container.add(panel1); } // 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 filecontent = textarea1.getText(); try { java.net.URL url = new java.net.URL(codeBase + servlet); java.net.URLConnection con = url.openConnection(); con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.writeUTF(filename); out.writeUTF(filecontent); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); } catch (IOException ioe) { } } // end savefile } // end class IdeaTool Can anyone please help me detect the error of the codes? Maybe there is logic error in passing variables from applet to servlet? Please help me! Thank you so much. Yours Sincerely, TEH NORANIS --------------------------------- Need Mail bonding? Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users. --0-147936322-1171530033=:3485--