Servlets and downloading files - java

I want to download an image file using a javascript client. Then I want to call the servlet in apache tomcat from the applet.
How do I host the file on tomcat. i.e. the same as hosting it in the 'docroot' folder on regular webserver?

Just drop the file in a subfolder of the /webapps folder. E.g. Tomcat/webapps/images/foo.png. It'll be available by http://localhost:8080/image/foo.png. Or if you already have a webapp, just drop it in the web folder (there where you also put your JSP files and where the /WEB-INF folder is also present).

Related

How to show link to download a file contained in deployment war?

I am deploying a war to a tomcat web server. (struts 1, not 2)
It needs to display a link for the user to download a static document that is to be included in the exported war.
What folder inside the deployment war should I store the document in, and how should the url link to the file?
http://localhost:8080/SampleWar/sample.html
consider sample.html file in webapp folder
your file should be inside webapp , but this way anyone can access no security , for example you can also have sample.txt or etc

Java application with simple Web Server exported as JAR

I have created a simple Web Server application using Java APIs.
Web server is working as expected and I am able to get the HTML pages in the browser.
I am developing this Java App on a Windows Machine. For testing I am exporting my app as JAR and then testing it on Target Device which is a Linux Box. In my app I have created a "webroot" folder and I am storing all the HTML files, that web server needs to serve.
So when I create JAR file of the app then it has "webroot" folder with all the html files in it. When I run this application on Windows then I am able to get the html pages. But when I run this application on Linux box as a JAR then I am not able to retrieve html files. Also when I copy my "webroot" folder outside the JAR then it works and I am able to see the HTML pages getting delivered in the browser.
So is there any way I can access html pages which are in the jar file itself without copying them outside?
My Project Folder structure is as below:
/src
-com.myprj.server -> contains server Java files
/webroot -> all the html pages
/bin -> jar files as per the above package path
/myprj.jar -> Project jar file
So above jar file has the webroot folder. And from the code I am accessing it as "webroot/FileName.html". If I keep webroot as in the same folder as jar then it does work.
Without seeing how you're actually serving the content, it's hard to say what you're doing wrong, but you can always use Class.getResourceAsStream() to access the resources from the classpath. If you're running it as a jar file, then the contents of the jar file are included in the classpath too.
To serve content outside of the jar file, either include your "webroot" in the classpath, or create some kind of mechanism to first try the classpath and then an outside path.

How to download zip file from tomcat server web app location

I am working on ios project. One of the module in that project download *.zip files and extract them into app memory. on the server side I installed tomcat server, and uploaded all *.zip files and some *.txt files to the context path.
But when I call the *.txt file's it's working fine (I am getting text on app and browser) but when I call *.zip's file I got 404 (the requested resource is not available). the url is correct but it's only working for .txt files and some extension files.
I think it's blocking .zip files for security reasons. But I need zip file need to downloaded.
if the .zip files is outside the server webapps path its need to be added on context path (and dir path), and reload the server solves the problem.

Why i can't see my js file from browser - tomcat 7

I deployed my ROOT.war to Tomcat webapps folder. When I'm writing in a web browser address www.exampleaddress.com I can see index.jsp file.
But in the same folder is file.js. I Would like to see it when I write address: www.exampleaddress.com/file.js but now i can see only empty white page without any code. Any ideas?
You can't place JS files or any publicly accessible data inside the WEB-INF folder. You need a JS folder in the same directory as your WEB-INF folder and that is where you place them. The same goes for your .css files and your images. None of that can be seen from inside WEB-INF.
/WebApp/CSS/index.css
/WebApp/JS/index.js
/WebApp/WEB-INF/index.jsp
/WebApp/Images/logo.png

Tomcat 404 for a folder in webapps

I have a folder called attachments in my webapps folder in my Apache Tomcat directory, in which I need to save some files and images. When I'm giving the local path i.e. in C:/ the files needed are being saved in the correct location.
However I need to load these images from the server at runtime so I'm trying to access the image by the localhost url /attachments/img.png. The image is not being found (404 error).
I tried opening Tomcat's manager to see if attachments is listed. It is there however when I click it a 404 error is being thrown too. Other deployed web applications are being found.
What could be the cause of this?
You cannot just create a folder in $CATALINA_HOME/webapps like this and access it from web browser.
webapps folder is supposed to be home for all web applications with proper J2EE web app like directory structure e.g. WEB-INF/web.xml, WEB-INF/classes, WEB-INF/lib, META-INF etc.
Read more about Web Application Directory Stricture

Categories

Resources