669489 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

EJB design EJB design EJB design Messages: 1 Messages: 1 Messages: 1 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

how to obtain the relative path of a resource in a j2ee project?

Posted by: Neeraj Lal on October 16, 2009 DIGG
I have a Dynamic Web Project having a flat file (or say text file). I have created a servlet in which I need to use this file.

My code is as following:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

if ( resource != null && !resource.equals ( "" ) ) {
//use getResourceAsStream ( ) to properly get the file.
InputStream is = getServletContext ().getResourceAsStream ("rateJSON") ;
\if ( is != null ) { // the resource exists
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( ) ;

for(int c = is.read(); c != -1; c = is.read( )) {
sw.write ( c ) ;
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
out.flush();
}
}

The problem is that the InputStream is has null value.

I'm not sure how to get the correct path?
I'm using JBOSS as the app server.
  Message #326246 Post reply Post reply Post reply Go to top Go to top Go to top

Re: how to obtain the relative path of a resource in a j2ee project?

Posted by: Gianluca Romeo on October 17, 2009 in response to Message #326126
If you have a look at the API you can read:

getResourceAsStream: Returns the resource located at the named path as an InputStream object.

The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path.

getResource: Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

...you might be asking yourself what's the context root?

it is the root of your web app folder (or archive) so if you put "file.ext" in WEB-INF then you have to use "/WEB-INF/file.ext"

Hope this helps

Recent active threads Recent active threads Recent active threads More More More
JGroups 2.8.0.GA released
How to display cell contents vertically in web report?
Small Java Development Company.. How to find work?
As EU ponders Oracle-Sun deal, MySQL creator sounds call to arms
SpringSource takes Spring 3.0 to GA
Seamless webapp deployment with Maven and Tcat Server
JSR-286 Now Posted
Language-oriented Programming and Language Workbenches
Dependency Injection JSRs - JSR-299 & JSR-330
Design-Time API Promises to make Java more like VB
More active threads »
Top posters of the weekTop posters of the weekTop posters of the week
This list contains the members who have made the most posts in all forums over the last 7 days:
  1. Chief Thrall
  2. augustientje bloem
  3. Reza Rahman
  4. Nathaniel Auvil
  5. Wayne Chang
Hot threads Hot threads Hot threads More hot threads More hot threads More hot threads

Object pooling is now a serious performance loss

Brian Goetz continues to lift the lid and peak into the inner workings of Java in Java Urban Performance Legends. In this article he exposes the fallacy behind some of the more common performance myths found in the annals of the JVM.
(93 comments, last posted February 06, 2009)

Beyond Java

Bruce Tate, author of Better, Faster Lighter Java and Bitter EJB has come out with a new book called Beyond Java. Bruce has an epiphany about the future of software development. Does it include Java?
(770 comments, last posted September 23, 2009)

Three forms of AJAX: solid, liquid and gas.

Looks like today AJAX concept have several interpretations. We can distinguish different approaches of AJAX integration. Can they co-exist within the same application? Can we talk about layered AJAX integration?
(68 comments, last posted May 08, 2008)

Design-Time API Promises to make Java more like VB

Artima has published a short article describing the Design-Time API for JavaBeans, which was recently approved as JSR 273. This API promises to bring VB-like ease to Java development, but may face a cultural bias among Java developers who tend to think more in terms of class libraries than components.
(226 comments, last posted December 24, 2009)

Will Sun be that target of a management buyout?

There is plenty of speculation today regarding a potential buyout of Sun Microsystems by Scott McNealy and Silver Lake Partners. How would privatization of Sun affect Java?
(16 comments, last posted May 15, 2009)
More hot threads »

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy      Powered by JIVE
Site Map