Eclipse Dynamic Web Project how to organize files? - java

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

Related

How to create a jar file for a web application using Netbeans IDE 8.0

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

how to deploy a eclipse dynamic web page in tomcat?

I developed dynamic web page in eclipse.I also configured apache tomcat in eclipse juno.In eclipse it run on server fine.I don't know how to deploy the dynamic web page in other system tomcat server.My folder structure.
Tomcat 7.0
-webapps
-Myproject-Folder
-WEB-INF
-classes
-lib
-web.xml
create a folder in your tomcat webapps folder
Then create another folder WEB-INF and index.html
Then in WEB-INF folder create three other folders classes,src,lib and web.xml
In classes folder keep all the .class files.
If you want to keep the source files then these should be placed in the src folder.
In the lib folder keep all the necessary jar files like activation.jar,mail.jar(if you are using java mail Api).
Now map correctly in web.xml
Now if your web.xml is correctly mapped then open a browser and type
http://localhost:8080/your_folder_name
8080 is the default port number.
just copy the "Myproject-Folder" to the "webapps" folder of any tomcat
Depending on how you want to deploy it, you can just copy the folder under "webapps" of your current Tomcat or package it into .war and put that under webapps folder of the target Tomcat.

Generic War file just like API concept

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.

How do I package a web fragment in a jar with Netbeans and Maven

When selecting a new maven project from the Netbeans New Project Menu, there is the option to create a Web Application, or a Standalone Application. I'd like to create a Class Library for use with a Web Application.
How would you recommend I go about this. I see the new Servlet 3.0 Web Fragments need to be put into the META-INF folder and saved as web-fragment.xml. I'm not too sure where I'd create this directory if I'm packing a JAR and a WAR.
Some insight would be greatly appreciated.
You can create either Java application or Web Application. Just place the META-INF folder in the src/main/resources folder. and place web-fragment.xml directly under it. You can also place any images,css or javascript files in META-INF/resources folder. So when the application you are deploying this for will look in the WEB-INF/lib folder and load all the META-INF contents of the jar onto the classpath.
when generating the .war file with standard maven settings (no special config of maven-plugin-war) the web-fragment.xml should found in the /META-INF dir of the generated JAR file inside the WAR's /WEB-INF/lib dir.
in our case the web-fragment.xml was not in this dir but in the WAR's /META-INF dir, which is the wrong place

Difference between jar and war in Java

What is the difference between a .jar and a .war file?
Is it only the file extension or is there something more?
From Java Tips: Difference between ear jar and war files:
These files are simply zipped files
using the java jar tool. These files are
created for different purposes. Here
is the description of these files:
.jar files: The .jar files contain
libraries, resources and
accessories files like property files.
.war files: The war file contains
the web application that can be
deployed on any servlet/jsp
container. The .war file contains jsp,
html, javascript and other files
necessary for the development of web
applications.
Official Sun/Oracle descriptions:
The J2EETM Tutorial: Web Application Archives
The Java Archive (JAR) File Format: The Basics
Wikipedia articles:
WAR file format (Sun)
JAR file
WAR stands for Web application ARchive
JAR stands for Java ARchive
A .war file has a specific structure in terms of where certain files will be. Other than that, yes, it's just a .jar.
You add web components to a J2EE application in a package called a web application archive (WAR), which is a JAR similar to the package used for Java class libraries. A WAR usually contains other resources besides web components, including:
Server-side utility classes (database beans, shopping carts, and so on).
Static web resources (HTML, image, and sound files, and so on)
Client-side classes (applets and utility classes)
A WAR has a specific hierarchical directory structure. The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static web resources are stored.
(source)
So a .war is a .jar, but it contains web application components and is laid out according to a specific structure. A .war is designed to be deployed to a web application server such as Tomcat or Jetty or a Java EE server such as JBoss or Glassfish.
A .war file is a Web Application Archive which runs inside an application server while a .jar is Java Application Archive that runs a desktop application on a user's machine.
A war file is a special jar file that is used to package a web application to make it easy to deploy it on an application server. The content of the war file must follow a defined structure.
.jar and .war are both zipped archived files.
Both can have the optional META-INF/MANIFEST.MF manifest file which hold informative information like versioning, and instructional attributes like classpath and main-class for the JVM that will execute it.
.war file - Web Application Archive intended to be execute inside a 'Servlet Container' and may include other jar files (at WEB-INF/lib directory) compiled classes (at WEB-INF/classes (servlet goes there too)) .jsp files images, files etc.
All WAR content that is there in order to create a self-contained module.
war and jar are archives for java files. war is web archive and they are running on web server. jar is java archive.
Basicly both compressed archives. war is used for web application with a specific directory structure.
A JAR file extension is .jar and is created with jar command from command prompt (like javac command is executed). Generally, a JAR file contains Java related resources like libraries, classes etc.JAR file is like winzip file except that Jar files are platform independent.
A WAR file is simply a JAR file but contains only Web related Java files like Servlets, JSP, HTML.
To execute a WAR file, a Web server or Web container is required, for example, Tomcat or Weblogic or Websphere. To execute a JAR file, simple JDK is enough.
War : For web-applications
Jar : For desktop applications
OR
War : Working on browser
Jar : Working on machine
War -
distribute Java-based web applications. A WAR has the same file
structure as a JAR file, which is a single compressed file that
contains multiple files bundled inside it.
Jar -
The .jar files contain libraries, resources and accessories files
like property files.
WAR files are used to combine JSPs, servlets, Java class files, XML
files, javascript libraries, JAR libraries, static web pages, and any
other resources needed to run the application.
Jar:- jar contain only .class
war:- war contain html, js, css and .class also jsp and servlets pages
JAR files allow to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.
JAR can be created with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.
A JAR file allows Java Runtime Environment (JRE) to deploy an entire application including the classes and the associated resources in a single request. On the other hand, a WAR file allows testing and deploying a web application easily.
Also, using an embedded container, you can run a JAR file directly whitouh setting up a web server like when running your java app with spring boot. However, for a WAR file, you need to set up first a web server like Tomcat for example.

Categories