Solved

Tomcat Webapps Read File

Posted on 2010-08-25
13
958 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
  • 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 27

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
 
LVL 27

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 27

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 27

Accepted Solution

by:
rrz earned 225 total points
ID: 33526182
>String filePath = context.getRealPath("/File.txt);  
error
String filePath = context.getRealPath("/File.txt");
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Assisted Solution

by:objects
objects earned 225 total points
ID: 33526998
0
 
LVL 27

Expert Comment

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

Assisted Solution

by:Hegemon
Hegemon earned 50 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
Updating Java 9 87
word0 challenge 4 52
pairs challenge 5 44
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now