Saturday, 17 November 2012

Appending user entered content to a file under webcontent using Servlets

//Appending user entered content to a file under webcontent using Servlets


import java.io.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This servlet exists to process POST method requests only.
 * If text data is supplied in a parameter called "postData", this will
 * be appended to a file called "postData.txt".
 * Once the data is appended, the entire contents of the file
 * are returned as a web page.
 */

public class PostServlet extends HttpServlet {

    private static File postDataFile;

    protected void doPost(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

        // Get the posted date from the request body
        String postData = request.getParameter("postData");//getting the user entered data

        // Set up the text file
        postDataFile =
            new File(getServletContext().getRealPath("/") + "postData.txt");//creating a file under webcontent

        // Append posted data to a file
        // ... which means saving the file contents temporarily first

        ArrayList al = new ArrayList();
        FileReader fr = new FileReader(postDataFile);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while ((line = br.readLine()) != null) {
            al.add(line);
        }
        br.close();
        br.close();
        // Now write the temporarily saved contents, and finish with the posted data
        FileWriter fw = new FileWriter(postDataFile);
        BufferedWriter bw = new BufferedWriter(fw);
        Iterator it = al.iterator();
        while (it.hasNext()) {
            line = (String) it.next();
            bw.write(line);
            bw.newLine();
        }
        bw.write(postData);
        bw.newLine();
        bw.close();
        fw.close();

        // Set appropriate content type for response, and obtain
        // the HTML output stream.

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // Set up start of HTML page
        out.write("<HTML>\n<HEAD>\n<TITLE>Post Servlet</TITLE>\n</HEAD>\n<BODY>");

        // Retrieve the contents of the "posted data" file, and write
        // back the contents to a web page.

        fr = new FileReader(postDataFile);
        br = new BufferedReader(fr);
        out.write("\n<P>");
        while ((line = br.readLine()) != null) {
            out.write("\n<BR />" + line);
        }
        out.write("\n</P>");
        br.close();
        fr.close();

        // Finish off the HTML page and close cleanly

        out.write("\n</BODY>\n</HTML>");
        out.close();

    }

    public void init() throws ServletException {
        String thisClass = this.getClass().toString();
        String thisServlet = this.getServletName();
        System.out.println(
            "Servlet "
                + thisServlet
                + " initialized: full class name is "
                + thisClass);
    }

}

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...