0

I'm advancing in jee programming. From basic tutorials if I want read my application directory in class extending HttpServlet I use getServletContext().getRealPath(relativePath). But In my case I used Jersey for serialization purposes. Servlet not extending HttpServlet, look at imageenter image description here . I'm mainly front programmer so maybe it is stupid question, but I want simple static/util method for resolving pathes to different static resources in my webapp. I run it on Tomcat. One and only servlet as on image.

RobertW
  • 1,111
  • 2
  • 12
  • 15

2 Answers2

2

As per specs (JAX-RS 1.1, ch. 6.1), add this in your resource class:

@Context
private ServletContext application;

The fully quallified names are:

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • Context bindinding is manged by container so if I tried use it servlet constructor or static method context was null. – RobertW Oct 30 '13 at 09:12
0

The absolute path to your webApp/WEB-INF/classes directory can be accessed as below:

URL resource = getClass().getResource("/");
String path = resource.getPath();

This will return you an absolute path like this:

/C:/SERVERS/x/y/x/yourApp/WEB-INF/classes

And from this you can get the path to the webapp directory:

path = path.replace("WEB-INF/classes/", "");

Now if your static resource is directly under webapp, you can access that resource with this:

path = path + "resource_name"; 
Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
  • Looks simple and right. I will try it later. Now I found that my ressource must be outside webapp direcoty. – RobertW Oct 30 '13 at 09:15