www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] JSP IO saved file location problem

  1. #1
    Join Date
    Mar 2007
    Location
    Egypt
    Posts
    65

    resolved [RESOLVED] JSP IO saved file location problem

    Hello,
    In a JSP I'm using this
    Code:
    getServletContext().getRealPath("/filename.extension");
    to set the file path and name of the generated file
    and it saves the file under Build/web directory while i want it in a directory near the JSP page. (for example www.site.com/images/)

    Can anyone help me with this?
    Thanks in advance.

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    I'm not sure I'm following you but if you just want to put it in a specific folder you can just add that to your variable you are retrieving that contains the web/build folder.

  3. #3
    Join Date
    Mar 2007
    Location
    Egypt
    Posts
    65
    Quote Originally Posted by criterion9 View Post
    I'm not sure I'm following you but if you just want to put it in a specific folder you can just add that to your variable you are retrieving that contains the web/build folder.
    Yeah, I understand your point but I'm not able to access the written file by providing the same path i got from getServletContext.getRealPath methods

    I want to either access it with Java IO again or just point to it with an HTML hyperlink

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Then you would repeat the same steps to figure out the directory (or reuse your variable) if you need to access it using IO and you just append "/images/" before the filename when you access it using a link/img/etc. Unless I'm still not following it really isn't all that hard.

  5. #5
    Join Date
    Mar 2007
    Location
    Egypt
    Posts
    65
    First, thank you for your responses
    Second, Yes, appending the folder to the path is no big deal but I'll explain more.

    In my NetbeansProjects folder there's my application folder named ExpensesTest
    under ExpensesTest there's Web folder and Build folder which has the built java after Netbeans deploys the application to the server

    When I write the file it goes under Build/web directory
    then when I try to access it from jsp page using getServletContext().getRealPath or application.getRealPath, the path would be:

    http://localhost:8080/ExpensesTest/<some-long-path>/Build/web/myfile

    which obviously doesn't exist.
    Is there a way to move the generated file from Build to the root? or there's a way to access the file after the build? or it'll just work when the application is deployed?

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    You wouldn't use getRealPath to display the image using a relative path:
    http://forums.sun.com/thread.jspa?threadID=5369494

    getRealPath is for writing the file, then you would use a relative path to read the file. Does that make sense?


    As an alternative you can store the image just about anywhere and use a servlet to handle locating and displaying the image as needed (this is highly flexible as you could then use a DB or alternate storage medium separate from the build directory). Here is some excellent code for an image servlet to handle images that are not stored within a web accessible directory:
    http://balusc.blogspot.com/2007/04/imageservlet.html

  7. #7
    Join Date
    Mar 2007
    Location
    Egypt
    Posts
    65
    Thanks, I'll check those two links.
    and yeah that does make sense

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles