?
Solved

Tomcat Webapps Read File

Posted on 2010-08-25
13
Medium Priority
?
984 Views
Last Modified: 2013-11-24
I have a web application using JSP that usea a JavaBean using Tomcat server.  The JavaBean must be able to read a file located in the webapps directory as readonly.  When using new File( filename), it doesn't located the file immediately in the webapps directory.  

How do I specifiy the filename so that new File will find the file in the webapps directory?  Since the Javabean isn't a JSP/servlet, it's harder to do that.
0
Comment
Question by:lcor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 10

Expert Comment

by:Hegemon
ID: 33523038
How is the web application deployed and what is its directory structure ?
0
 
LVL 28

Expert Comment

by:rrz
ID: 33523304
In your web app's web.xml file  you could have something like  

    <servlet>      
                   <servlet-name>AppContext</servlet-name>
                   <servlet-class>rrz.AppContextServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  

and the servlet could  be something like  

package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppContextServlet extends HttpServlet {
    public void init(ServletConfig config){
                   try{
                       super.init(config);
                       AppContextHolder.set(config.getServletContext());
                       System.out.println("init of AppContextServlet"); //for testing
                      }catch(ServletException se){System.out.println("error in AppContextSrevlet init");}
    }
}

and the class could be something  like  

package rrz;
import javax.servlet.ServletContext;
public class AppContextHolder{
   private static ServletContext currentContext;
   private static String dirPath;
   public static void set(ServletContext context) {
                                                   currentContext = context;
                                                   String appPath = context.getRealPath("/");
                                                   int index = appPath.indexOf("webapps");
                                                   dirPath = appPath.substring(0,index + 7);
   }
   public static ServletContext getContext(){
                                             return currentContext;
   }
   public static String getDirPath(){
                                             return dirPath;
   }
}

and in your bean you could use  
String dirPath = AppContextHolder.getDirPath();
File f = new File(dirPath);
0
 

Author Comment

by:lcor
ID: 33525858
It's a typical Tomcat deployment

webapps/mywebapp/File.txt
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:rrz
ID: 33526105
Sorry I am guess I mis-understood
>The JavaBean must be able to read a file located in the webapps directory as readonly  
but if the file is inside your web app's root directory,
>webapps/mywebapp/File.txt  
then  
In your web app's web.xml file  you could have something like  

    <servlet>      
                   <servlet-name>AppContext</servlet-name>
                   <servlet-class>rrz.AppContextServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  

and the servlet could  be something like  

package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppContextServlet extends HttpServlet {
    public void init(ServletConfig config){
                   try{
                       super.init(config);
                       AppContextHolder.set(config.getServletContext());
                       System.out.println("init of AppContextServlet"); //for testing
                      }catch(ServletException se){System.out.println("error in AppContextSrevlet init");}
    }
}

and the class could be something  like  

package rrz;
import javax.servlet.ServletContext;
public class AppContextHolder{
   private static ServletContext currentContext;
   public static void set(ServletContext context) {
                                                   currentContext = context;
   }
   public static ServletContext getContext(){
                                             return currentContext;
   }
}

and in your bean you could use  
String context = AppContextHolder.getContext();
String filePath = context.getRealPath("/File.txt);
File f = new File(filePath);
0
 
LVL 28

Expert Comment

by:rrz
ID: 33526148
If you are new to java, then I should give more details.
Change my package name (rrz) to the package that contains your bean.
Please ask if you need more help.
0
 
LVL 28

Accepted Solution

by:
rrz earned 900 total points
ID: 33526182
>String filePath = context.getRealPath("/File.txt);  
error
String filePath = context.getRealPath("/File.txt");
0
 
LVL 92

Assisted Solution

by:objects
objects earned 900 total points
ID: 33526998
0
 
LVL 28

Expert Comment

by:rrz
ID: 33527211
He wants to access from his bean.
0
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 200 total points
ID: 33529358
I think anything related to request URL or servlet context (e.g. getRealPath()) is not related to the problem. The author needs to read a file stored INSIDE his web application, which can be deployed as an exploded application or .WAR archive and their locations can vary.

I think the only good way to access the file in this condition is like below:

ClassLoader cl = Thread.currentThread().getContextClassLoader();
BufferedReader br - new BufferedReader (new InputStreamReader(cl.getResourceAsStream("WEB-INF/somefile.xml")));

I'd like to reiterate, only if the file is included into the web app's structure. If it is external to it, use File as you would normally do.

Another not so good way would be fiddling with Tomcat properties and Tomcat API calls trying to find out how and where the application is deployed (an unpacked into a temporary dir it it was deployed as a WAR) and deriving an absolute path to the file.
0
 
LVL 92

Expert Comment

by:objects
ID: 33529376
> The author needs to read a file stored INSIDE his web application,

thats what getRealPath() is for.
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33529388
getRealPath() - .. This method returns null  if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
0
 
LVL 92

Expert Comment

by:objects
ID: 33529421
yes, in a war won't work. afaik it cannot be achieved with a war
0
 

Author Closing Comment

by:lcor
ID: 33535310
just used realpath and passed the path to the bean.

Classloader solution was informative but not the solution I used.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question