Resource/File URL Problems, relative links, servlet context and

Discussion in 'Java' started by LB, Jun 2, 2008.

  1. LB

    LB Guest

    I'm trying to write to a file from within a <jsp:useBean java class.
    Problem is the naming conventions of the file address. I'm trying to
    develop application on localhost using netbeans, then deploy to live

    I can't figure out how to pass the servlet context info into the
    useBean java class.

    (attempt 1)
    I found a posting that recommended adding this code into the JavaBean:
    import javax.servlet.jsp.JspPage;

    private JspPage jspPage;

    public JspPage getJspPage() { return jspPage; }
    public void setJspPage (JspPage jspPage) { this.jspPage =
    jspPage; }

    and then, from within the JSP page, invoke the following:
    <jsp:useBean id="yourBean" class="your.package.YourBean" />
    <% yourBean.setJspPage(this); %>

    Then the bean can access the implicit objects and methods of the
    calling JSP page. Reportedly, you can use the ServletConfig using
    JspPage.getServletConfig() and in turn use that
    with .getServletContext() to return the ServletContext related to the
    original JSP page.

    No go for me: I keep getting "Non-static method getServletConfig()
    cannot be referenced from a static context." errors. Ugh.

    (attempt 1.5)
    I tried same technique with a <%
    yourBean.setServContext(this.getServletContext) but I get immediate
    errors on the .jsp page... No go there.

    (attempt 2)
    I was able to successfully transmit String contextPath and String
    hostName from the JSP using an already existing form:
    <INPUT TYPE="hidden" name="contextPath" value= $
    {pageContext.request.contextPath} >
    <INPUT TYPE="hidden" name="hostName" value=<%=
    request.getHeader("Host")%> >
    Getters & Setters are defined within my bean, and the next .jsp page
    includes <jsp:setProperty name="yourBean" property="*" />

    Problem is, I am unable to resolve those into an address that works
    for the file system. I was hoping to use
    ServletContext.getRealPath(string) function, but I don't think that's
    in the cards.

    I do have the following:
    hostName = "localhost:8080"
    contextPath = "/ProjectName"
    filePath = "/data/file.csv"

    with netbeans local server running I can manually paste
    http://localhost:8080/ProjectName/data/file.csv and see the correct
    file. What I have been unable to do is joint those three strings
    together and avoid a

    PrintWriter userout = new PrintWriter(new FileWriter(new
    File( this.realCSVFilePath),true));

    When realCSVFilePath = "http://localhost:8080/ProjectName/data/
    file.csv", the error that I get on the server is:
    " http:\localhost:
    8080\ContactUs_Feedback\data\VisitLog.csv (The filename, directory
    name, or volume label syntax is incorrect)"

    One slash after http:// disappears and all the slashes are reversed.
    Anybody been here before? I've been playing with the
    without much luck.

    Any clues as to what I am doing wrong? I'm embarrassed to tell you
    how long I've been struggling on this problem. Many thanks in
    LB, Jun 2, 2008
    1. Advertisements

  2. LB

    LB Guest

    I got some of the file stuff cleaned up using class URLConnection and

    URL tempURL = new URI(PROTOCOL + hostName + contextPath +
    URLConnection CSVconnect = tempURL.openConnection();
    CSVconnect.setRequestProperty("Content-type", "application/

    DataOutputStream DataOut = new
    BufferedReader DataIn = new BufferedReader(new
    String inputLine;
    while ((inputLine = DataIn.readLine()) != null) {

    DataOut.writeBytes("writeBytes " + getCSVText());
    DataOut.writeChars("writeChars " + getCSVText());
    DataOut.writeUTF("writeUTF " + getCSVText());

    I can now see the text file that I have stored within my Netbeans
    project. I can access the file using: "http://localhost:8084/
    WebApplication1/data/textfile.csv" I can see the system.out.println
    listing on my local host server per above and it correctly matches the
    file of interest...)

    What will not work is any DataOut operation. How do you write to a
    simple text file from a 'network / served' application? I've tried
    both DataOutputStream and PrintWriter. Obviously with those two
    different classes, the write / append methods vary slightly...) I'm
    not getting any exception errors, and yes, my code should catch
    something. The file write operation simply doesn't seem to be
    working. I'm using Netbeans IDE 6.1 on a Windows XP machine.

    Anybody been here before? I can read the file, but can't write to
    it. Is there some permission issue on a windows localhost machine?
    Did you all give up and just simply attach all your data to a SQL
    database instead? How do you send data to a simple text file at the
    server from a POJO bean?

    thanks in advance for your assistance,
    LB, Jun 10, 2008
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Jack Jia
    Apr 4, 2004
  2. Andrew Purser
    Alex Kay
    Nov 4, 2004
  3. Replies:
    Stuart Wood
    Jun 6, 2006
  4. Replies:
    Apr 20, 2007
  5. Garrett Smith
    David Mark
    May 26, 2009

Share This Page