Hello

Your problem might stem from two issues that I can see. One, in ServletIdea.java you're output stream is trying to open C://. I'm no expert, but you might want to try a single forward slash. Java should convert the single forward slashes to single backslashes for you. The other issue might be that URL.openConnection might not be trying to post the data, but instead might be sending the data as a get request. I've seen stranger things.

I hope that helps

Zack

Teh Noranis Mohd Aris wrote:
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("<html>");
  out.println("<head>");
  out.println("<title>Login</title>");
  out.println("<center><h2>Servlet Idea</h2>");
  out.println("<applet width=500 height=400");
  out.println("name=\"IdeaTool\"");
  out.println("codebase=\"/\"");
  out.println("code=\"IdeaTool\">");
  out.println("<param name=\"servlet\" value=\"" +
              req.getRequestURI() + "\">");
     out.println("<param name=\"id\" value=\"" +
                 session.getId() + "\">");
     out.println("</applet>");
out.println("</center></body></html>"); } 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+"<P>Next");
      out.println("filecontent="+filecontent);
     }
else{ out.println("requestparams.no-params"); }
     out.println("<P>");
     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.


---------------------------------------------------------------------
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