users@glassfish.java.net

Re: reading text file in servlet

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Fri, 13 Mar 2009 16:57:26 -0700

On 03/12/09 09:01 PM, glassfish_at_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
> <sun-web-app>
> <context-root>/testing-war</context-root>
> <property name="alternatedocroot_1" value="from=/testing/* dir=C:/Dev/java/NetBeans_projects"/>
> .....
>
> 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_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>