So I'm learning web application development and I'm a bit confused about how to organize my files in the template provided by eclipse for Dynamic Web Application.
Should I place my HTML pages in WebContent? Where do I put the JSP and servlet source files? What convention is globally followed?
This is the structure of the template provided by eclipse
Deployment Descriptor
JavaScript Resources
WebContent
ECMAScript Built-In Library
ECMA 3 Browser Support Library
src
JRE System Library
Apache Tomcat v8
Web App Libraries
EAR libraries
build
WebContent
META-INF
WEB-INF
lib
Place HTML and JSP pages in the WebContent directory.
The Eclipse help page referenced by android-weblineindia states that WebContent is:
The mandatory location of all Web resources, including HTML, JSP, graphic files, and so on. If the files are not placed in this directory (or in a subdirectory structure under this directory), the files will not be available when the application is executed on a server. The Web content folder represents the contents of the WAR file that will be deployed to the server. Any files not under the Web content folder are considered development-time resources (for example, .java files, .sql files, and .mif files), and are not deployed when the project is unit tested or published.
so CSS files and JS files needed by the browser should also be placed here.
Place java source files (so including servlet sources) in the src directory. The same help page states this for the source directory (although it suggests it is called JavaSource which looks like an error in the documentation):
Contains the project's Java source code for classes, beans, and servlets. When these resources are added to a Web project, they are automatically compiled and the generated files are added to the WEB-INF/classes directory. The contents of the source directory are not packaged in WAR files unless an option is specified when a WAR file is created.
If you look at project Properties -> Deployment Assembly you should see that files compiled from src are deployed to WEB-INF/classes and files in WebContent are deployed to the root of the WAR file
I did a Web Application using Netbeans IDE 8.0 in JAVA platform. The project consists of JSP's, Servlets and normal .java files. I would like to deploy the project as a simple jar file to the client so that they can execute the jar file and use the application. I am trying to generate jar file using Netbeans with the given resources but there were no proper resources for this. When I tried one of the sources provided, a war file is created rather than a jar file.
So, please help me in converting my web application into a jar file. Also, please let me know why a war file is created for my application rather than a jar file. Also, let me know how to execute war file.
Java applications use jar files and java web-applications use war files. JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform. WAR file (Web application ARchive) is a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web application. Since the structure of a web application is very much different from a typical java standalone application you need to deploy it into a war file. To run a WAR file you need a java EE based server like apache tomcat, jboss, glassfish etc. Steps required to run the war file are (One of the solutions possible, although there are many other -as pointed out by boris):-
1.Start the web server;
2.Deploy the war file to the web server;
3.Visit JSP pages or servlets in the browser by entering thier links to their path.
see this link for more details on deploying war :- deploy war file in tomcat server
WAR file is one of the common ways to deploy a java web application. What other deployments formats available for Java Web Applications? What are the differences between them?
Besides WAR, the enterprise applications can be wrapped within the so called "enterprise archive", which extension is ear. Here's a list of the archive types, in which the different pieces of a web-application are wrapped.
JAR: EJB modules which contain enterprise java beans class files and EJB deployment descriptor are packed as JAR file(s) with .jar extenstion
WAR: Web modules which contains Servlet class files, JSP Files, supporting files, GIF and HTML files are packaged as JAR file with .war (web achive) extension
EAR: All above files(.jar and .war) are packaged as JAR file with .ear ( enterprise archive) extension and deployed into Application Server. Contains also third party libraries. Has a special directory called APP-INF that contains the application.xml file descriptor.
AS you may know that when an API is prepared, it can be used in any project just by adding it into classpath in J2SE. I wonder if it is possible to create a web page and compiled it into a war file just like an API definition so that It can directly be used within an ear.
No, you can't do that using a war file, because a war file would define a new web application separated from the other ones.
But since the servlet 3.0 spec, you can do that with a jar file placed in the WEB-INF/lib directory of your war file, using the META-INF/resources directory of the jar. See this question for details.
What is the difference between a .war and .ear file?
A WAR (Web Archive) is a module that gets loaded into a Web container of a Java Application Server. A Java Application Server has two containers (runtime environments) - one is a Web container and the other is a EJB container.
The Web container hosts Web applications based on JSP or the Servlets API - designed specifically for web request handling - so more of a request/response style of distributed computing. A Web container requires the Web module to be packaged as a WAR file - that is a special JAR file with a web.xml file in the WEB-INF folder.
An EJB container hosts Enterprise java beans based on the EJB API designed to provide extended business functionality such as declarative transactions, declarative method level security and multiprotocol support - so more of an RPC style of distributed computing. EJB containers require EJB modules to be packaged as JAR files - these have an ejb-jar.xml file in the META-INF folder.
Enterprise applications may consist of one or more modules that can either be Web modules (packaged as a WAR file), EJB modules (packaged as a JAR file), or both of them. Enterprise applications are packaged as EAR files ― these are special JAR files containing an application.xml file in the META-INF folder.
Basically, EAR files are a superset containing WAR files and JAR files. Java Application Servers allow deployment of standalone web modules in a WAR file, though internally, they create EAR files as a wrapper around WAR files. Standalone web containers such as Tomcat and Jetty do not support EAR files ― these are not full-fledged Application servers. Web applications in these containers are to be deployed as WAR files only.
In application servers, EAR files contain configurations such as application security role mapping, EJB reference mapping and context root URL mapping of web modules.
Apart from Web modules and EJB modules, EAR files can also contain connector modules packaged as RAR files and Client modules packaged as JAR files.
From GeekInterview:
In J2EE application, modules are packaged as EAR, JAR, and WAR based on their functionality
JAR:
EJB modules which contain enterprise java beans (class files) and EJB deployment descriptor are packed as JAR files with .jar extension
WAR:
Web modules which contain Servlet class files, JSP Files, supporting files, GIF and HTML files are packaged as a JAR file with .war (web archive) extension
EAR:
All the above files (.jar and .war) are packaged as a JAR file with .ear (enterprise archive) extension and deployed into Application Server.
war - web archive. It is used to deploy web applications according to the servlet standard. It is a jar file containing a special directory called WEB-INF and several files and directories inside it (web.xml, lib, classes) as well as all the HTML, JSP, images, CSS, JavaScript and other resources of the web application
ear - enterprise archive. It is used to deploy enterprise application containing EJBs, web applications, and 3rd party libraries. It is also a jar file, it has a special directory called APP-INF that contains the application.xml file, and it contains jar and war files.
WAR (web archive) files contain servlet class files, JSPs (Java servlet pages), HTML and graphical files, and other supporting files.
EAR (enterprise archive) files contain the WAR files along with the JAR files containing code.
There may be other things in those files but their basically meant for what they sound like they mean: WAR for web-type stuff, EAR for enterprise-type stuff (WARs, code, connectors et al).
JAR Files
A JAR (short for Java Archive) file permits the combination of several
files into a single one. Files with the '.jar'; extension are utilized by
software developers to distribute Java classes and various metadata.
These also hold libraries and resource files, as well as accessory files
(such as property files).
Users can extract and create JAR files with Java Development Kit's
(JDK) '.jar' command. ZIP tools may also be used.
JAR files have optional manifest files. Entries within the manifest file
prescribe the JAR file's use. A 'main' class specification for a file class
denotes the file as a detached or ‘stand-alone' program.
WAR Files
A WAR (or Web Application archive) files can comprise XML
(extensible Markup Language) files, Java classes, as well as Java Server
pages for purposes of Internet application. It is also employed to mark
libraries and Web pages which make up a Web application. Files with
the ‘.war' extension contain the Web app for use with server or JSP (Java
Server Page) containers. It has JSP, HTML (Hypertext Markup
Language), JavaScript, and various files for creating the aforementioned
Web apps.
A WAR file is structured as such to allow for special directories and
files. It may also have a digital signature (much like that of a JAR file)
to show the veracity of the code.
EAR Files
An EAR (Enterprise Archive) file merges JAR and WAR files into a
single archive. These files with the ‘.ear' extension have a directory for
metadata. The modules are packaged into on archive for smooth and
simultaneous operation of the different modules within an app server.
The EAR file also has deployment descriptors (which are XML files)
which effectively dictate the deployment of the different modules.
Refer: http://www.wellho.net/mouth/754_tar-jar-war-ear-sar-files.html
tar (tape archives)
- Format used is file written in serial units of fileName, fileSize, fileData
- no compression. can be huge
Jar (java archive)
- compression techniques used
- generally contains java information like class/java files. But can contain any files and directory structure
war (web application archives)
- similar like jar files only have specific directory structure as per JSP/Servlet spec for deployment purposes
ear (enterprise archives)
- similar like jar files. have directory structure following J2EE requirements so that it can be deployed on J2EE application servers.
- can contain multiple JAR and WAR files
Ear files provide more options to configure the interaction with the application server.
For example: if the hibernate version of the application server is older than the one provided by your dependencies, you can add the following to ear-deployer-jboss-beans.xml for JBOSS to isolate classloaders and avoid conflicts:
<bean name="EARClassLoaderDeployer" class="org.jboss.deployment.EarClassLoaderDeployer">
<property name="isolated">true</property>
</bean>
or to src/main/application/META-INF/jboss-app.xml :
<?xml version="1.0"?>
<jboss-app>
<loader-repository>
loader=nameofyourear.ear
<loader-repository-config>java2ParentDelegation=false</loader-repository-config>
</loader-repository>
</jboss-app>
This will make sure that there is no classloader conflict between your application and the application server.
Normally the classloader mechanism works like this:
When a class loading request is presented to a class loader, it first
asks its parent class loader to fulfill the request. The parent, in
turn, asks its parent for the class until the request reaches the top
of the hierarchy. If the class loader at the top of the hierarchy
cannot fulfill the request, then the child class loader that called it
is responsible for loading the class.
By isolating the classloaders, your ear classloader will not look in the parent (=JBoss / other AS classloader).
As far is I know, this is not possible with war files.
J2EE defines three types of archives:
Java Archives (JAR) A JAR file encapsulates one or more Java classes, a manifest, and a descriptor. JAR files are the lowest level of archive. JAR files are used in J2EE for packaging EJBs and client-side Java Applications.
Web Archives (WAR) WAR files are similar to JAR files, except that they are specifically for web applications made from Servlets, JSPs, and supporting classes.
Enterprise Archives (EAR) ”An EAR file contains all of the components that make up a particular J2EE application.
To make the project transport, deployment made easy.
need to compressed into one file.
JAR (java archive) group of .class files
WAR (web archive) - each war represents one web application
- use only web related technologies like servlet, jsps can be used.
- can run on Tomcat server
- web app developed by web related technologies only jsp servlet html js
- info representation only no transactions.
EAR (enterprise archive) - each ear represents one enterprise application
- we can use anything from j2ee like ejb, jms can happily be used.
- can run on Glassfish like server not on Tomcat server.
- enterprise app devloped by any technology anything from j2ee like all web
app plus ejbs jms etc.
- does transactions with info representation. eg. Bank app, Telecom app