How to include html files in a jar? - java

So, the question is rather simple, and might be supid.
I have created a simple webserver, for which i need to deploy to a Azure, virtual machine.
I have created a jar file, and a bat file to start the server on the file.
In my creation of the webserver, i have a filehandler, and some html pages.
Is there any way to include the html pages in the final jar file for deployment ?

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 to hot deploy newly created files to Apache Tomcat in Eclipse IDE?

I have a peculiar problem. I am extracting the data from a database in the form of .csv file and then passing this file to d3.js for visualization.
The problem is, d3.js doesn't allow file:// based protocol (so I cannot give path to .csv file directly). The .csv file needs to be located on the server. But this file is generated on the run time.
I tried dumping the file in the same project folder, but as expected, the Eclipse doesn't take the updated file until next clean and build action.
Any idea how to work around this problem?
You need to place the file in the place where your application is deployed on Tomcat (e.g.: Tomact7\webapps\myProject...), not in Eclipse Workspace location, this way your js can access the File either by its URL or relative path.

Website directory structure different from eclipse

I am coding a website using java servlets and am using eclipse and tomcat. When I test it using localhost, it works fine. But when I am deploying it on my actual website, the directory structure is messed up and the files are not called properly.
My eclipse directory structure on localhost is
Project Name
.src/packageName/java files
.WebContent/HTML files.
When I make a call from the html files, I use the relative location and tomcat automatically knows to look in the src/packageName folder. For example, from the /WebContent/login.html page makes a onClick call as follows,
. This will automatically trigger the java file in /src/packageName/welcome
When I am deploying it in my actual website, the WebContent/login.html is throwing an error WebContent/welcome file is not found. How do I tell my website to search in /src/packageName folder?
Hmm...have you been sure to package the application as a war for deployment.

tomcat and extracted files

I am using ubuntu with packaged tomcate7 .
I used .war file to upload the website.
the web is working fine.
This is not my website but its there:
:/var/lib/tomcat7/webapps/ROOT$ ls
index.html META-INF
I find my site files (those in .war) at
/var/cache/tomcat7/Catalina/localhost/_/org/apache/jsp/
/var/cache/tomcat7/Catalina/localhost/_/WEB-INF/classes/
I do not find any of my .css or .js files with a system wide search
like find / -name '*.css' -ls
where are they?!
Is there an easy way to just give tomcat the .class files or delete the .jsp .java files
(for example if I don't want to give the real source but only .class)
JSP precompilation. During build-time some tool (jspc) is difficult.
any easy walk through?
The WAR file is likely "somewhere" on the server, but tomcat may have been configured to not "explode" the WAR files on deploy, and simply serve the resources directly out of the WAR. That may be why your web site "works" but you can not find the actual .css files, the .css files are not there on the file system but still contained in the WAR.
As for #2, pre-compiling JSPs, for some reason this is still a "jump through flaming hoops" process that is only semi-automated.
Pre-compiling JSPs before deployment entails the converting of the JSP file to a Java source file, compiling that file, and then mapping that resulting class as a servlet in the final web app, and then finally removing the JSP file from the web app.
There is a Tomcat Page that discusses the process, but only offers part of the solution (from an automation stand point).
Of course, one reason pre-compiling is not popular is that pre-compiled JSPs are not portable across containers, nor necessarily versions of the same container. So, that's a notable issue.

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