• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reading File

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to read a jsp frile from a servlet. My Jsp is in the root directory of the web application. By giving what path, I can access that file.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it just reading the file or transferring the request execution to the jsp...

If its reading then
you can use getResourceAsStream()
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to read the contents. Noe it is showing FileNotFoundException.
How should I give the path. Let my jsp is WebContent/Jsps/hellop.jsp where WebContent is the root of application
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using

getResourceAsStream("/Jsps/hellop.jsp")
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the jsp is in the context then try

getResourceAsStream("/hellop.jsp")

Its a method from servlet context.

InputStream in = getServletContext().getResourceAsStream("/hellop.jsp");
[ March 28, 2007: Message edited by: Rahul Bhattacharjee ]
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can read it using a File or FileReader class. But first you need to get the path to your JSP files, and this can be done by getting the path from the servlet context.

 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by I Wayan Saryada:
I think you can read it using a File or FileReader class. But first you need to get the path to your JSP files, and this can be done by getting the path from the servlet context.



If the file exists within the structure of the web app, using FileReader with getRealPath is a poor (or, at least, non-portable) choice.

getRealPath returns null if your application has been deployed as a packed war file. Using getResourceAsStream works the same way whether the app has been deployed as a packed war file or as an expanded file system.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic