Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

File Content Not Saved To Server

Teh Noranis Mohd Aris

2007-03-01

Replies:

Dear All,
 
I want to save a file name and its content to the computer server at directory C:/temp/. I've passed the file name and file content parameter from the applet to the servlet. The problem now is that the file name is created in the computer server directory but a 'null' is included in the file content. Can anyone please help me solve the problem why this is happening?
Thank you so much.
 
The applet is as follows:
 
// AppletSave.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 AppletSave 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/ServletIde";
 try
{
servletUrl = new URL(servletName + "?filename="+filename+"&teditor="+teditor);
con = servletUrl.openConnection();
con.setUseCaches(false);

BufferedReader buf = new BufferedReader(new InputStreamReader(con.getInputStream()));
}

catch(Exception e)
{
System.out.println("Exception caught..."+e);
}
       
 } // end savefile
} // end class AppletSave
 
The servlet is as follows:
 
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletSave 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>Save File To Server</h2>");
out.println("<applet width=500 height=400");
out.println("name=\"AppletSave\"");
out.println("codebase=\"/\"");
out.println("code=\"AppletSave\">");
out.println("<param name=\"servlet\" value=\"" +
        req.getRequestURI() + "\">");
out.println("<param name=\"id\" value=\"" +
        session.getId() + "\">");
out.println("</applet>");
out.println("</center></body></html>");
 
 
  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


---------------------------------
The fish are biting.
Get more visitors on your site using Yahoo! Search Marketing.
©2008 junlu.com - Jax Systems, LLC, U.S.A.