aspose file tools*
The moose likes Servlets and the fly likes Reading File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading File" Watch "Reading File" New topic
Author

Reading File

Jose Mon
Ranch Hand

Joined: Feb 13, 2007
Posts: 30
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

Joined: Jul 04, 2004
Posts: 979
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

Joined: Feb 13, 2007
Posts: 30
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

Joined: Mar 27, 2006
Posts: 245
Try using

getResourceAsStream("/Jsps/hellop.jsp")


Vikrant Pandit
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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 ]

Rahul Bhattacharjee
LinkedIn - Blog
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

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.



Website: Learn Java by Examples
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reading File