Results 1 to 8 of 8
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default [SOLVED] writing to WEB-INF while on server.

    I am writing a utility that is supposed to run on the server. It has to write a file on init() or find one there and leave it as it be. Last work yesterday results in a shell properties of 181 mb on the file it seemed to generate, but NetBeans servlet output ran it such that the file wrote to the root of the sample web application. This file needs to be placed under WEB-INF for design reasons. I do not wish to ftp this file to the server, I want something like an app with a main() - a utility - that runs during servlet init and either generates the data if it is not there or loads it if it is. This file will be created once and never changes during Tomcat restarts.

    It cannot go in / it has to go under WEB-INF

    any ideas welcome
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    It cannot go in / it has to go under WEB-INF
    Confused. What does under mean?

    C:\asdf\wer\ddd.txt - here ddd.txt is in wer
    where would under be?

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post root

    base dir, in a string that looks like folders and so on, is sometimes called root. That terminiology, the four characters
    Java Code:
    char[] name = {'r','o','o','t'};//
    has historical origins that cause it to be often associated with a single slash ( forward or backward )

    A single slash, all by it's self, is the ultimate root of the filesystem on the Unice. ( plural of Unix ) and all mounts descend from there. Root has special priviledges that make it best to restrict the account as much as is resonably possible.

    Unless one is a system engineer with a soldering tool and a bench manual, all file mounts have a \ or / at the beginning of the string. We could do a primitive filesystem by making "/filename".hashCode() the key for a Map or Hashtable.

    By stating 'under WEB-INF' I would be saying
    Java Code:
    /name/name/WEB-INF/name/name
    such that the file write, on disk, is needful to show up to the right of WEB-INF in the above string.

    IOW:
    /base/dir/WEB-INF/anotherDir/ ... the file I need ....
    or
    /base/dir/WEB-INF/ ... the file I need ....
    not
    /folder/myFileINeed
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    under
    is that like saying more than one subdirectory below being in the folder?
    C:\asdf\wer\sub1\xxx.txt - here xxx is under wer

    Have you tried the different combos of paths in a File... class to see where the file is written on the server?
    Last edited by Norm; 07-17-2008 at 04:03 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    Here is code that writes a file in a directory on a Tomcat server. Here is the URL to call ShowSource: http://127.0.0.1:8080/Norms/jsp/source.jsp?TestFile.txt
    Heres the jsp that calls the Servlet:

    <%@ taglib uri="http://java.apache.org/tomcat/examples-taglib" prefix="eg" %>
    <eg:ShowSource jspFile="<%= request.getQueryString() %>"/>

    This is the servlet that calls TestDummy which does the writing.

    Java Code:
    public class ShowSource    extends TagSupport {
        String jspFile;
        
        // NCR added to see if another class can be called
        public ShowSource() {
          TestDummy tdc = new TestDummy("ShowSource");
        }
    And here is TestDummy.java. It was an iterative process to find out where the file would be written. I get the current directory from System props and then wrote a relative path from there.
    Java Code:
    // Called from ShowSource Constructor
    
    package examples;
    
    import java.io.*;
    
    public class TestDummy {
    	public TestDummy(String who) {
    		System.out.println("TestDummy " + who + " user.dir=" + System.getProperty("user.dir"));
          //                                        user.dir=C:\Java\jakarta-tomcat-3.2.4\bin
          // Create and write a file
          try {
             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                 new FileOutputStream("../webapps/Norms/Testing/NewFile.txt")));
    //"../webapps/Norms/Testing/NewFile.txt" ->C:\Java\jakarta-tomcat-3.2.4\webapps\Norms\Testing\NewFile.txt
    //"Testing/NewFile.txt" ->  C:\Java\jakarta-tomcat-3.2.4\bin\Testing\NewFile.txt         
             out.write("This is a test");
             out.newLine();
             out.write('H');
             char[] buf = {'e', 'l', 'l', 'o'};
             out.write(buf);
             out.newLine();
       
             out.flush(); // for illustration only; close() will flush
             out.close();
          }catch(Exception ex) {
             ex.printStackTrace();
          }
    	}
    }
    Last edited by Norm; 07-17-2008 at 05:01 PM.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Smile details

    Quote Originally Posted by Norm View Post
    is that like saying more than one subdirectory below being in the folder?
    Like your
    "C:\asdf\wer\sub1\xxx.txt" - here xxx is under wer.
    Yes, like that is what under is intended to convey in the original post and clarification at 5:11
    Quote Originally Posted by Norm View Post
    Have you tried the different combos of paths in a File... class to see where the file is written on the server?
    That was my original plan. I posted first, not expecting the caliber of work I see from you here. I am up against the wall just trying to use your information. A few short clarifications so that I may use your information effectively.

    What is NCR, as in:
    Java Code:
    // NCR added to see if another class can be called
    To grasp:
    Java Code:
    <%@ taglib uri="http://java.apache.org/tomcat/examples-taglib" prefix="eg" %>
    <eg:ShowSource jspFile="<%= request.getQueryString() %>"/>
    I grabbed http://archive.apache.org/dist/java/...1.1.2-docs.zip and am in an ineffective effort to grasp the path of logic through these jsp tags to ShowSource then to TestDummy at
    Java Code:
    new FileOutputStream("../webapps/Norms/Testing/NewFile.txt")
    What I see here ( all caveats apply ) is a System.out.println(.....) to see where we are at, then the gains of your efforts in
    Java Code:
    // It was an iterative process to find out where the file would be written.
    from which I am taking the initial step to be mimic the code in my own test stubb, see where the file write shows up and then go back and fathom how to implement the server side includes in the <% %> such that I am not reliant on an external server.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    NCR - my initials. I comment the mods I make to sample/other peoples code to know how/where I've changed it.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post some first results

    okay, what I have so far is
    Java Code:
    C:\Documents and Settings\Owner\My Documents\NetBeansProjects\WebApplication1
    from:
    Java Code:
    java.io.PrintWriter pw
    =
    new java.io.PrintWriter(new java.io.FileWriter("Error.log"));
    try
    {
            // Initialize kg for 8 bit bytes * 8 bytes.
            KEY_GENERATOR.init(56);// number of bits, not numbytes
            //
            System.out.println(KEY_COUNT.toString());
            //
            pw.append((CharSequence) new String( System.getProperty("user.dir")));//
            pw.flush();//
    //.........
    The directory name "WebApplication1" is sample app in NetBeans. I suppose for the moment I will need to cross paste into Servlet code and run it in Tomcat. The ide will do that with a button click.

    Vol 2 Core Servlets and Java Server Pages is shipping. That is a reference, it carrys the Sun Imprint. First Vol sold 100,000 unexpectedly.

    Message Edit:
    had to get routine working to work on this, so far - so good:
    Java Code:
    // eight byte longs, 4k of them....
    -6395698419861245149
    -5912023626293063525
     7472960202950636626
    -2968649499680953189
     4876631316236996329
    Last edited by Nicholas Jordan; 07-17-2008 at 09:13 PM. Reason: partial results
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Replies: 2
    Last Post: 07-15-2008, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •