Skip to main content

[webtier] Re: reading text file in servlet

1 reply [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jan Luehe

On 03/12/09 09:01 PM, glassfish@javadesktop.org wrote:
> Jan,
>
> Thanks for the reply. While I understand what is in the blog, I don't think I know how to apply it to my case. May be I am not that knowledgable on the relationship between URI and file. Let me put it in a more specific way:
>
> I have this in my sun-web.xml
>
> /testing-war
>
> .....
>
> What should I code, in my servlet's doGet() method so that I can open the text file and read the data? And where in absolute path should I put my text file?
>
> I've tried new File(new URI("/myFile.txt")) but failed with "URI is not file:". I've tried new File(new URI("file:/myFile.txt")) but it looks for the file in c:\myFile.txt, not in the "dir" in my property. I've tried new File(new URI("file:myFile.txt")) but failed with "URI not hierarchical ". Please help.
>

In your servlet, you can acquire a URL to the resource that is stored in
your
alternate docroot, as follows:

getServletContext().getResource("/testing/test.txt");

This will return a URL to this resource:

C:/Dev/java/NetBeans_projects/testing/text.txt

Hope this helps.

Thanks,

Jan

> [Message sent by forum member 'senderj' (senderj)]
>
> http://forums.java.net/jive/thread.jspa?messageID=336774
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net