?
Solved

Tomcat Webapps Read File

Posted on 2010-08-25
13
Medium Priority
?
977 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

765 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