//package staff2.grewe.servlets; // /usr/local/tomcat/logs import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class WriteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int value; Process myProcess; Runtime myRuntime; PrintWriter out = response.getWriter(); out.println("Attempting to Open file"); try{ //IDEA 1: open directly in user's public_html directory //append if file exists //NOTE: directory you are writing into make mode 707 // this means it is readble by others (including nobody //the owner of the servlet process) but, not readable or writeable //by other students. //Note: servlet is not user grewe so, // file mode had to be 7*7 and had to exist first //Odd: above directories did not have to by 7*7 // 755 was fine FileWriter fw = new FileWriter("/staff2/grewe/public_html/comments/g.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println("Hi Lynne"); out.println("hi"); pw.close(); /* //IDEA 2: open in servlet's current directory and then copy // file to user's directory //PROBLEM: can open and create file in servlet's directory evne //while file did not exist. However, to be able to copy it //over to the user's directory then you need to have //that directory be writeable by all 7*7 //Note: this will make the file mode of 744 FileWriter fw = new FileWriter("fun.txt", true); PrintWriter pw = new PrintWriter(fw); out.println("Attempting to write"); pw.println("Hi Lynne"); out.println("hi"); pw.close(); //tried to move over file myRuntime = Runtime.getRuntime(); //myProcess = myRuntime.exec("cp fun.txt /staff2/grewe/public_html/comments/fun.txt"); myProcess = myRuntime.exec("mv fun.txt /staff2/grewe/public_html/comments/fun.txt"); InputStream myIS = myProcess.getInputStream(); out.println("Copying of file finished"); while (( value = myIS.read() ) != -1) { //print out the character not the number value. out.print((char) value); } //now wait for process myProcess.waitFor(); */ }catch (Exception e) {out.println(e.getMessage()); } } }