Is there a way to retrieve the absolute path of url (http://localhost:8080/myApp) in java. The scenario is, i need to connect to csv file located in tomcat server. The statement works well if I enter the absolute path, but is there a solution to retrieve url's path using getAbsolutePath().Sorry if I'm wrong.
Connection conn = DriverManager(getConnection("jdbc:relique:csv:/home/apache-tomcat-6.0.26/webapps/myApp/"))
Thanks in advance.
You can use ServletContext.getRealPath(), which does exactly what you want.
Note that it does not necessarily work in all situations. For example, if your Tomcat is configured to deploy the .war file without unpacking it, then this will return null.
I don't know much about JAVA.
May be getServletContext().getContextPath() is something you are looking for
EDIT:
Or may be getRealPath()
Tomcat is not a http server. All tomcat urls reference services, not files.
You'll have to implement another service that sends the csv file on request, if you want to get it through any http URL. URL's like http://localhost/myapp/input.csv require a http server like apache httpd.
(Hope I got your question correct...)
Related
I have a website and now I want it to be able to work offline. I'm trying using service-worker to implement this function, but unfortunately, my website is written in multiple .jsp files. So when one jsp jumps to another jsp, the internet is required, as jsp is not able to be resolved in client browsers. Is there a way to solve this problem? Or do I need to rewrite all the jsp to one file?
Thanks in advance.
If your site is running on your own computer, configure it to accept requests on 127.0.0.1. Then you can use it when you are disconnected from the internet.
If the problem is that your JSPs link to each other via absolute URLs using your server's DNS name you could:
change the JSPs to use relative URLs, or
(hack!) temporarily tweak your DNS resolver to resolve the server DNS name to 127.0.0.1.
In general, it is not possible to convert a JSP to a static page, and still have it work properly.
I am working within Java, and downloading files from a HTTP Server. Now we are working with symlinks here, so we do not need to change the http link - it is always pointing to "last-uploaded.zip" which is linked to the last uploaded zip file, as an example "package43.zip".
Do I have the chance to get the original filename within java? So the link is pointing to "last-uploaded.zip" but if it is downloaded I want to rename it to "package$version.zip".
Regards,
Marco
No. The whole symlink concept doesn't transfer over HTTP, so when you make a HTTP GET for last-uploaded.zip you don't know if it's a file, a symlink or just an endpoint that returns bytes.
The simplest solution is probably opening the zip and searching for the version number from inside there somewhere.
I have created a jersey web-service in Java and I have the following issue:
When I run the project in my localhost apache tomcat server, I call it with the URL localhost:8080/api/verify? and I haven't any problem.
When I upload it in my web-hosting and I call it with URL myDomainName/api/verify? I get 404 error.
Does anyone know why it's happened?
Thank you in advance
Check your web service to be deployed and successfully started (on 80 port).
Check your hosting security settings to allow access to /api/*.
Please try what others have suggested and also check if your domain name (myDomainName) is the correct one. If you have packed your projected as a war check the name of the generated folder.
Check your DNS server, your firewall, and your load balancer to make sure they are pointing to your app.
Verify if you are using http or https.
Edited to add: make sure your default tomcat port 8080 is there in the load balancer (if you have one). If you don't have one yet try http://yourDomainName:8080/api/verify and see if that's all you need.
Thank you all for your responses.
The problem was resolved by my web hosting support.
The solution was the following:
He changed the mappings (mod jk tomcat connector ) from .jsp to / to fix the issue.
I am running Tomcat 5.0 and I have problem with getting a resource from tomcats 'webapps' from Java. There is a html file that I check if it's available after server start in
tomcat_folder/webapps/myProject/site.html
In Java I run this code when server is started:
URL url = new URL("http://localhost:8080/myProject/site.html");
URLConnection con = url.openConnection();
Object content = con.getContent();
getContent() throws FileNotFoundException.
But when I put "http://localhost:8080/myProject/site.html" into browser, the site is displayed without problems.
Also I tested that on 4 machines - on 2 everything is fine, on other 2 FileNotFoundException.
I thought it might be some folder security access problem or user rights, but in the end I have no clue. Any suggestions?
Assuming you are on windows. You may need to add java.exe to your virus scanner / firewall to allow it to make out bound network connections.
If your code is in servlet then you can get the real path of your servlet and then append the file name to it.
String servletPath = getServletContext().getRealPath("/");
URL url = new URL(servletPath+"site.html");
For normal java application you will need the entire path to the webapps folder of your tomcat installation.
Your code seems to be correct.
but you missed to actually connect
con .connect();
before trying to getContent
I found the solution to my problem.
The cause was a windows service using port 8080, which was also in use by Tomcat. Resource was available through browser, but not from Java code.
After stopping conflicting service everything works as it should now.
While deploying my app to mochahost, I met the problem between servlet and GWT-RPC communicate. The error shows:
HTTP Status 404 - /403.shtml
type Status report
message /403.shtml
description The requested resource (/403.shtml) is not available.
.war file works perfectly on my workstation, but not working on mochahost.
Any ideas to solve it?
Thanks in advance.
Mochahost have a very good support, try livechatting with their tech department, you will probably have the thing solved.
That's what I do.
Make sure you update live site URL. For instance, generally, on local system you access web app as http://localhost:8080/myapp but, on server it changes to http://[www.]myapp.com. Again, this is just an instance. The point is, the live site must reflect correct URL from code (servlet/JSP/action/etc...) and configuration properties, if any.
Comment 'DirectoryIndex' property in .htaccess file if you do not have any index file.
Comment 'RewriteCond' property in .htaccess file if you do not have any rewrite requirement.
For sure, one of the reason - if client does not accept cookies and servlet does not encode URL.