Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading File

 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using

getResourceAsStream("/Jsps/hellop.jsp")
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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 ]
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic