Tomcat: How to point different directories to the same WEB-INF - java

Currently trying to setup multiple subdomains/domains in my Tomcat and I'm needing them all to use the same WEB-INF/classes/ for everything.
Basically my folder structure is like so:
Z:/
project/
assets/ (assets.domain.com)
main/ (www.domain.com)
dev/ (dev.domain.com)
WEB-INF/ (the WEB-INF I want everyone using.)
classes/
com/
example/
So basically I need assets.domain.com, www.domain.com, and dev.domain.com to go up one level in the directory to find the WEB-INF and use the Java classes stored there... Is there any way to accomplish this? Thanks!

if you are trying to share java code across webapps, you will need to package them as jar and place it in either server/lib or endorsed folder . then each deployed webapp will have access to the same class files.

Related

Preferred File Location for Static Files in Maven WAR Project

I am trying to access some WSDLs and XMLs files from WebApp which i built in Maven as a WAR. Now I knew that the resources folder was the default location for these types of files, but when I checked the war package, I found that the files ended up inside the /WEB-INF folder, which is of not much use. Just wanted to know where do I put these files so that I can access them via URL:
https://server:port/Context/File_Path
I tried to make a folder inside webapp but outside WEB-INF, it did seem to do the trick. But just wanted to know if that the right approach.
Maven has a concept of standard directory layout
So, yes, in order to work with something that can be packaged into WAR with maven-war-plugin you should follow the convention and place the files into the relevant folders (of course you can change these defaults if you wish).
When it comes to the static resources, indeed there is a special src/main/webapp folder. So you're right

Properly deploying a small Java project

I'm currently working on a small Java Project (~30 Classes, 5 external libs).
The code accesses resources in the folders src/resources and src/test_resources using getClass.getResouce("/resources/any.file").
Most of these resource files will probably never be touched by a user, but there are also some regular configuration files which are intended to be edited by the end users.
My question now is: How should I be deploying such an application?
Exporting everything into a runnable jar doesn't seem to be a good way, as I don't wanna torture my users and let them unzip the jar for editing the configuration files.
Should I export all of the internal stuff into the jar, and copy the resources directory into a Folder side by side with it? How can I access the resources then?
Thank you guys!
You could copy the resources folder. It doesn't necessarily need to be side-by-side with the jar file. The key is that you need to put the parent folder of the resources folder on the classpath.
For example, you could copy it someplace like:
c:\some\directory\resources
Then, when you execute, do something like:
java -cp c:\some\directory;c:\some\path\to\your.jar;... your.main.ClassName

folder structure to be used for an application with extjs and java?

I have created a Java Project wherein I am using ExtJS for the web pages and I should be using Java for the programming logic. Right now,I am able to display the web pages. However, I do not have any java files and my current folder structure is:
ext_java_proj
src
jre system library
apache tomcat v7.0
build
WebContent
com
controller
model
store
view
BorderLayout.js
ext_js
resources
src
ext_all.js
index.html
Can someone tell me if I should place my java files in src folder or I should place it within the WebContent>com folder. And which is the best practice if it is a relatively big project with lot of java files and js files.
Thanks in advance.
Java files should be kept in src folder. You shouldn't put any java code inside Webcontent folder as it is accessible to the client. The reason why we keep Javascript/ExtJS files in Webcontent is that these files should be accessible to the end user.
Note: Build tools read java classes from src folder convert them into classes and put them in WEB-INF folder which will never be accessible to the client. Structure of src folder depends on the build tools. If you are using eclipse (since it uses ant) you keep your java files directly in src folder. In case of maven you should keep them in src/main/java folder.

How to read/write H2 embedded DB to a file inside your JAR?

I have an Eclipse project that is organized as follows:
MyProj/
src/
main/
java/
<All main Java source here>
config/
spring/
spring-config.xml
views/
<All JSPs here>
h2/
my-h2-db
test/
java/
config/ } etc. for test sources
When I bundle it into a WAR, it is packaged like so:
MyProj.war
META-INF/
WEB-INF/
web.xml
lib/
classes/
spring-config.xml
my-h2-db
views/
Home.jsp
About.jsp
etc.
com/ --> root dir for all compiled Java binaries
I need to write code that is capable of CRUDing an H2 database (and all the tables, users, etc. that go with it) that for this example I have called my-h2-db. This way the app can work with the same DB file no matter if I'm testing from inside Eclipse, or if I'm actually using the deployed WAR.
So I ask:
How do I access the my-h2-db file both from the Eclipse and runtime (WAR) classpath, since it is packaged in a different location than where it lives in the Eclipse project?; and
I don't think you'll want to package the H2 files within your WAR application. It's not going to be able to use them in the WAR.
What you want in your version control is the DDL used to build the database entities (and pre populate if necessary). Then, either in your development or production environments, you use these scripts to create/update the database.
What you have in your web application is simply the JDBC url (specifying the file location of the database) used to locate the embedded database. As you suggested, this will be different based on development or production environments.
I've been using LiquiBase as a way of managing database creation/upgrades, and it's pretty easy to use.
However, the idea of creating the database as part of development and then embedding those files within the WAR in order to use them directly won't work. There's no concept of saving those files, so none of the data would persist.

Overriding WAR classes directory

If you use the Ant to create your WAR via the war task, then (as explained here) it will automatically create a WEB_INF/classes/ directory in the WAR. I would like to change the name of this generated classes/ directory to, say, classpath/ and can't figure out how.
If this is possible, would someone provide a code example as well as a reference to the documentation that shows how to do this (I'm curious!). Thanks!
You shouldn't change the name of the classes to something else.
The directory structure of a WAR file is standard and defines a WEB-INF\classes for all the .class files of your application

Categories