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

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

Related

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 maven knows about my web app files vs servlet files?

I created a simple Maven web app project, with 1 jsp file index.jsp. I am able to access this file from server as follows:
http://localhost:8080/myProject/
How does it is able to access my index.jsp ? I haven't specified anything on web.xml. Where does all these configuration was exist ?
2.) Can I change the URL to like http://localhost:8080/myProject/webapp/index.jsp ?
If you're using the Maven Standard Directory Layout, then everything located in the Web application sources directory (src/main/webapp) will be in the root of the application and will be public.
If you want to create /myProject/webapp/index.jsp, then create a webapp folder inside the src/main/webapp folder, so you will have folder structure like src/main/webapp/webapp (Which I don't see any reason to use it that way). Then create the index.jsp inside it.

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

deploy war file in tomcat server

I have copied the sample.war file in webapps directory of tomcat.
I can acess localhost:8080.
deploying of wars is automatic by default -i have checked my webapps folder for an extracted folder "sample"
but it is not extracted.why the war file is doesn't extracted.please give me solution for this.
Delete that war file from webapps/ directory. Then open link http://localhost:8080/ in your web browser. Click Tomcat Manager then enter user name and password. In next page you can see one option called "WAR file to deploy". Select your war file from there and click "deploy" button.
If you want auto deployment when you copy files to webapp/ directory. Then make sure that you server.xml file contains the following values:
autoDeploy="true"
unpackWARs="true"
Just search these values in your server.xml file and edit values as shown above
For more info see Deploy A New Application from a Local Path.
In general this happens when you have 2 Tomcats. If CATALINA_HOME is referring to Tomcat-A and you are trying to run Tomcat-B from Tomcat-B/bin/startup.bat. It will run Tomcat-A. Hence your war which you are trying to deploy in Tomcat-B will not be extracted as Tomcat-A is running in actual.

Servlets and downloading files

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).

Categories

Resources