#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    34
    Rep Power
    15

    Question


    How can I write information into a file on my server in java servlets?
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    go to the Java API and look up the java.io package. you write to a file from a servlet the same way you write to a file from any other java code (except applets). i advise using a FileWriter wrapped in a BufferedWriter. good luck. here's another link to the servlet API. the APIs are your best friends. when i have a question about how to implement something, i go there and 90% of the time I find the answer.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    34
    Rep Power
    15

    Question


    I've already tryed a FileWriter wrapped in a BufferOutputStream. I have the following code:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.net.*;


    public class MindmapWWWBoard extends HttpServlet {

    String username;
    String message;
    private Writer out2;

    public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    postReceipt(request, out);
    }

    private void postReceipt(HttpServletRequest request,
    PrintWriter out) {
    username = request.getParameter("username");
    message = request.getParameter("message");
    out.println("<html>n" +
    "<head>n" +
    "<title>n" +
    "Your Postn" +
    "</title>n" +
    "</head>n" +
    "<body>n" +
    "<b>Username:</b> " + username + "<br>" +
    "<b>Message:</b> " + message +
    "</body>n" +
    "</html>");
    try {
    FileWriter fw = new FileWriter("mindmap.html", true);
    BufferedWriter out2 = new BufferedWriter(fw);
    out2.write(message);
    out2.flush();
    out2.close();
    }
    catch (IOException e) { }
    }
    }

    Does anyone know why this isn't working? Thanks in advance.
  6. #4
  7. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    what is the behavior? are there any error messages?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    34
    Rep Power
    15

    Cool


    It compiles ok, but when I run it on the server, it doensn't write to the HTML file. It shows the username and message after post.
  10. #6
  11. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    how about this:
    Code:
    File file = new File("/home/your/path", "mindmap.html");
    byte[] fileBuffer;
    
    FileOutputStream outstream;
    FileInputStream instream;
    
    int fileLength;
    if (file.exists()) fileLength=(int)file.length();
    else fileLength=0;
    
    // buffer for your message + file's contents
    fileBuffer = new byte[message.length() + fileLength];
    
    // stream in the file to the buffer
    if (file.exists()) {
     try {
         inStream = new FileInputStream(file);
         int numRead = 0;
         int counter = 0;
         while (numRead!=-1 && counter<fileLength) {
            numRead = inStream.read(fileBuffer, counter, fileLength-counter);
            counter += numRead;
         }
      }
    catch...
    }
    
    byte[] messageBytes = message.getBytes();
    //add message to the file buffer
    for (int j=fileLength; j<fileBuffer.length; j++)
        fileBuffer[j] = messageBytes[j-fileLength];
    
    //shove fileBuffer back into file
    try {
        outStream = new FileOutputStream(file);
        outStream.write(fileBuffer);
        outStream.close();
    }
    catch...
    that's bound to have a bug or two but i'm sure you can fix it up.

IMN logo majestic logo threadwatch logo seochat tools logo