Get path to servlet container deploy/webapps folder - servlets

From within the service method of a servlet, I can get the path to the web application's root folder with getServletContext.getRealPath(""), e.g. /tomcat/webapps/MyWebApplication, but is there any way I can get the path to /tomcat/webapps/ or optionally a specific other web application like /tomcat/webapps/MyOtherWebApplication (without resorting to hacks like substracting the context path)?
(Using only TomCat as an example, should work across all servlet containers.)

ServletContext has a getContext(String) method (javadoc) that return the ServletContext corresponding to another webapp, which you could then call getRealPath() on. However, the appserver's security model might forbid this, so use with caution.

Path to Tomcat dir (tomcat/bin):
File inputFile = new File("file.xml"); // tomcat_dir/bin/file.xml
Path to other Tomcat directories, "webapps" for example:
File inputFile = new File("../webapps/file.xml"); // tomcat_dir/webapps/file.xml
Nice way to get rid of the absolute paths :)

Related

Need to get real path to the Servlet i'm using. JAVA

I need to get path to the servlet that i'm using inside of it. With some searchings i found something like this:
System.out.println(getServletContext().getRealPath("/"));
and the result is:
C:\Users\me\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\ProjectName\ServletName
tbh i dont know what does ".metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps" mean, i just want to get path to the "original" project which is:
C:\Users\me\workspace\ProjectName\ServletName
is there any way to get this path directly(without getting the big path first and then cutting the middle part of it)?
The result you get is the location of the servlet .class file. Eclipse deploy your application in your server applications folder .metadata/.plugins/.../wtpwebapps (because you use the temporary folder which is different from your real Tomcat location), after invoking the function getRealPath("/"), your servlet has been deployed in the server and it has not been executed from it's original location. Even if you change tomcat's directory, you'll always get the location of the deployed servlet and not the .java file.
read more about Server temporary location.
you can also use ${pageContext.request.contextPath}.This command point the root folder of the project.

Change "~/foo" to "approot/foo" from inside Global.asax

In the global.asax of my MVC app, I have a string like "~/foo". I have to generate a client-side script (JavaScript) and pass the value of this path "~/foo" to that script.
However, before passing that path, I want that the path must resolve to "approot/foo" where approot is the application's root.
So, for e.g. I deploy my application in IIS under a new website named Ding (for want of a better word), the path "~/foo" must resolve to "/Ding/foo".
How do I do this?
You can use the VirtualPathUtility class there's a bunch of methods there to help you.
In your case you want VirtualPathUtility.ToAbsolute().
For example:
var path = VirtualPathUtility.ToAbsolute("~/foo");
Should resolve how you want.

Get application root URL and Directory name in Symfony 2 application

I want to know the root of the application both system eg. /var/www/app/ or /app/ for the purposes of file uploads. When I upload, I believe I need the "system" directory name, for use to links in web front end, I need the "relative" path. How do I get this information?
In a previous project, I defined constants in the app*.php files (front controller)
define('APP_ROOT', __DIR__);
Wonder if theres a better way?
In any ContainerAware class you can get location of web directory by,
$this->container->get('kernel')->getRootdir().'/../web';
If the container is not available, you can use dependency injection: set %kernel.root_dir% as an argument to your class in the service configuration.

Java EE - Best way to get real path to uploaded files? [duplicate]

This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 6 years ago.
I have a web application with an upload form where users can upload files.
I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.
Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";
But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext() and thus throws a nullpointer on getRealPath
Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.
Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...
I'd like this folder to be placed directly under the webapp-root.
This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.
You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads. You can configure this path in a properties file or as a system property.
I've found that a few ways to accomplish this regardless of what OS platform you're using. I typically like to store my files in some way relative to my web application. This being said, the easiest way to do this is using your class file as a reference to get the real path. I use something similar to the following to store image uploads for a few web applications that I've built:
public class FileLocationTest {
public static void main(String[] args) {
String path = FileLocationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println(path);
}
}
This can be done using a static utility class in your web app, or any class for that matter to obtain the real path of your application regardless of OS. Alternatively in a servlet I've also used the request class to get the Tomcat location if my appbase is in a different area, something like this:
String location = request.getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String path = location.substring(0, location.indexOf("/tomcat")) + "/data/events/images/";

How to get the path of the context directory in jsp and how can i overwrite it for every new request

Hi i am getting path of the context directory in my local system using..
String myfile = application.getRealPath("/");
but the method getRealPath("/") is returning null when application is deployed in war file in www.eatj.com..
can any one provide me possible solution and sample code please...
The purpose is i have to create a xml file in my context directory...
and each request i have to overwrite this xml file...
This is what Javadoc says
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. 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).

Resources