How to get exact path of servlet where it is running? - servlets

I have an application that I use in a servlet. The application assumes the text database residing in the same directory where it is being executed. When I am trying to use it in servlet and even after placing the text database files in /WebContent, /DataProject and also src folders. The application cannot find the database. I need to know exactly where the servlet file is being executed so I can place the database files in the same directory. I have already /.metadata/.plugin......../tmp0/wtpwebapps directory. Any help will be greatly appreciated.

Usually when we access files in java we give the absolute path. Dont use the relative path. We use relative paths for jsp/html/css etc. For accessing normal files use the complete path. So put the files in /home/tomcat/.../../directoryDatabase

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.

File Path and Root issues

So I have my the path to my website code as follows:
C:/folder1/folder2/folder3/my published website code from VS2012 - on my website I get an attachment and I want to save it to the following path C:/folder4
when I try the following code: file.SaveAs(Server.MapPath("../../folder4/") + filename); it says that I am going past the root. Can someone explain to me what is going on and if and how I can solve this issue?
Server.MapPath() is used to get the path in relation to the server root. Since your trying to save it outside the server virtual directory, you could probably just hardcode the file.
file.SaveAs(#"C:/folder4/" + filename);
It might not work depending on your IIS worker pool permissions.
file.SaveAs(Server.MapPath("folder4/") + filename);
Because I cannot see your folders structure I would reccomend setting a breakpoint after Server.MapPath() to see the full URI Path to determin your next steps since it says you are past root you may have one to many "../" before your string.
As per the documentation for HttpServerUtility.MapPath:
you cannot specify a path outside of the Web application
which is exactly what you are trying to do. If you interpret "the root" to be the root folder of your application, that is even what the error message is telling you.
Either
use an absolute path or
store your data beneath the application folder
use MapPath("~/") to get the current directory and build a relative path from that (in essence, you just move the "../.." outside the call to MapPath)
I would probably recommend going with 2. as it will give less headaches wrt. permissions and multiple sites hosted on the same server.
Server.MapPath(...) tries to return a physical ("real") directory for the virtual or relative path you give it. And since a virtual directory can't be located "over" the root in that sense, what you're trying to do makes no sense. You can go from domain.com/somefolder to domain.com/, but you can't really go any farther back.
You could instead use Environment.CurrentDirectoryas the starting point to find your folder, and apart from that just use SaveAs(..) as you're already doing.

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

FileHelpers file access

I am trying to get my head over FileHelpers library and have main critical problem. When I try to implement it in my web app, or even use online demo I end up with "FileNotFoundException". The chosen file is being looked for on my C: drive. How can I make the FileHelpers code to access relative path to my application instead of absolute one?
Regards,
Bartosz
Use the Server.MapPath() method to map a relative path (based on current directory or web-site root) to an absolute accessible path.
For example, if yourfile.txt is placed inside App_Data folder of your web-site then you can write:
Customer[] customers =
(Customer[])engine.ReadFile(Server.MapPath("~/App_Data/yourfile.txt"));
The tilde character represents the root of your web-site, if you specify a relative path then it'll be resolved as relative to the directory where your ASP.NET page resides.

ASP.net relative paths not working?

This should be pretty simple but it's not working.
I have a file underneath the root of my project. I want to call it like this.
GetWorkbook("tplBud806_wRevenue.xls")
I publish the project out to the server and try to run it and the server says it can't find it.
Could not find file 'c:\windows\system32\inetsrv\tplBud806_wRevenue.xls'.
That's not the path it should be taking. It should be under E:\IIServer\rootwww\reports\tplBud806_wRevenue.xls.
I thought relative paths were supposed to start from the path that the project was running in. I've also tried.
GetWorkbook("/tplBud806_wRevenue.xls")
GetWorkbook("\tplBud806_wRevenue.xls")
GetWorkbook("~/tplBud806_wRevenue.xls")
GetWorkbook("~\tplBud806_wRevenue.xls")
Is there some setting I'm missing? It's got to be something simple...
GetWorkBook(Server.MapPath("tplBud806_wRevenue.xls"));
GetWorkbook is not an ASP.NET function, and it likely defaults to the folder that the process calling it was started from. The process in this case is an IIS process and probably started in that folder.
Server.MapPath?
Your application is running in an AppDomain loaded by the w3wp.exe located in the directory in your error. Which means that trying to look for any file will start in that directory. You should use Page.MapPath, as mentioned by others. It tells the application to start looking in the folder your aspx is in.
GetWorkBook(Server.MapPath("~/tplBud806_wRevenue.xls")); If the .XLS file is at the root of your project.
You can use also use ~ in conjuction with ResolveURL() to access an URL in your site. So ~ will be replaced by the root URL of your project
Example:
ResolveURL("~\tplBud806_wRevenue.xls")
will be transformed to
http://myproject.url/website/tplBud806_wRevenue.xls
If you need disk access, like in your example, use Server.MapPath
Look at this SO post to learn more about Server.MapPath

Resources