title: how to obtain the relative path of a resource in a j2ee project
body: <p>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.</p>
  
  <p>My code is as following:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
     // String resource = request.getParameter ("json") ;
               if  ( resource != null &amp;&amp; !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();
                   }
            }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is that the <strong>InputStream is</strong> has null value.</p>
  
  <p>I'm not sure how to get the correct relative path.
  I'm using JBOSS as the app server.</p>
  
  <p>I have added the resource file in the WebContent directory of a Dynamic Web Project.
  As a different approch, I tried this:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
  
          ServletConfig config = getServletConfig();
          String contextName = config.getInitParameter("ApplicationName");
          System.out.println("context name"+ contextName);
          String contextPath = config.getServletContext().getRealPath(contextName);
          System.out.println("context Path"+contextPath);
          //contextPath = contextPath.substring(0, contextPath.indexOf(contextName));
          contextPath += "\\rateJSON.txt";
          System.out.println(contextPath);
  
      String resource = request.getParameter ("json") ;
      System.out.println("Hi there1"+resource);
               if  ( resource != null &amp;&amp; !resource.equals ( "" )  )   {
                   System.out.println("Hi there");
                  //use getResourceAsStream (  )  to properly get the file.
                  //InputStream is = getServletContext ().getResourceAsStream (resource) ;
                  InputStream is = getServletConfig().getServletContext().getResourceAsStream(contextPath);
  
  
                  if  ( is != null )   {  // the resource exists
                      System.out.println("Hi there2");
                       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 ) ;
                           System.out.println(c);
                       }
                      PrintWriter out = response.getWriter();
                      out.print (sw.toString ()) ;
                      System.out.println(sw.toString());
                      out.flush();
                   }
            }
    }
  </code></pre>
  
  <p>The value of contextPath is now: C:\JBOSS\jboss-5.0.1.GA\server\default\tmp\4p72206b-uo5r7k-g0vn9pof-1-g0vsh0o9-b7\Nationwide.war\WEB-INF\rateJSON</p>
  
  <p>But at this location the rateJSON file is not there? It seems JBOSS is not putting this file in the App.war or doesn't deploy it???
  Could someone please help me?</p>