/* * @(#)FileServlet.java (Metawerx Hosting examples) * * Author: Neale Rudd (MetaWerx) * Family: Example software * Type: servlet * Purpose: Demonstrate read/write from datafile in servlet root folder * Location: SERVLETROOT * JarFile: n/a * Target: JDK1.1x * * Copyright (c) 1998-2005, Metawerx Pty Ltd. All Rights Reserved. * PO Box 1114, Huntingdale, VIC, 3166, AUSTRALIA * All rights reserved. * http://www.metawerx.net * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * Synopsis * ===================================================================================== * This servlet attempts to write a file called file-servlet.txt in the servlet root * folder, then reads it back and displays the contents. * * Uses * ===================================================================================== * - demonstration of how to get the root folder location dynamically * - reading a file from Java * - writing to a file from Java * * History * ===================================================================================== * 05/06/2002 09:39 Neale Rudd * Created * */ import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import javax.servlet.*; import javax.servlet.http.*; public class FileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/plain"); out.println("File Servlet"); out.println(); String location = "start"; try { location = "Getting context"; out.println("- "+location); ServletContext context = getServletContext(); location = "Getting folder"; out.println("- "+location); String path = getServletContext().getRealPath("/file-servlet.txt"); location = "Opening output file: "+path; out.println("- "+location); PrintWriter f = new PrintWriter(new FileOutputStream(path)); location = "Writing data"; out.println("- "+location); f.println("Test from FileServlet"); f.close(); location = "Opening for input: "+path; out.println("- "+location); BufferedReader f2 = new BufferedReader(new InputStreamReader(new FileInputStream(path))); location = "Reading data"; out.println("- "+location); String s = f2.readLine(); f2.close(); location = "Read text: ["+s+"]"; out.println("- "+location); out.println("---- end of test ----"); } catch(Exception e) { out.println("Exception occurred, location: "+location); out.println("Stack trace: "); e.printStackTrace(out); } catch(Error e) { out.println("Error occurred, location: "+location); out.println("Stack trace: "); e.printStackTrace(out); } } }