There are two types of web apps that developers can create in Java: static and dynamic applications. Static web apps render the same exact content whenever a client asks for it, while dynamic web apps allow for particular content to be created for each web page. This can be useful in cases where you would like different users to view different information.
Reading: Best Kanban Tools for Developers
A static page is usually an html file (or a jsp file). However, when it comes to dynamic web pages, web developers need a servlet to create a page whenever a user makes a request to the server.
This programming tutorial covers how to build a simple dynamic web app for your server using Java. You will also need to use a server such as Tomcat or Glassfish as well. We will be using the Tomcat server for the example in this tutorial.
What is the Standard Directory Structure in Java?
When creating web applications in Java, it is important to follow the J2EE directory structure. This will ensure that the application server knows where to find the files it needs. Here is the directory hierarchy you should follow when creating a Java web app:
MyWebApp/ index.jsp index.html images/ audios/ WEB-INF | |__web.xml | |__ classes/ | |__lib/
In the root directory of your web app, you have file called index.html/ index.jsp files, as well as the WEB-INF directory. Outside the WEB-INF directory, developers can also include resource folders to hold things like images or audio files. These contents are automatically downloaded to a user’s client when they request the default page of the web application.
In your WEB-INF directory, you will find the web.xml file and two directories: classes and lib. The web.xml file is the web deployment descriptor and it maps URLs to a given resource.
The next section will discuss how to use web.xml file. The classes directory holds your servlets, while the lib directory contains the JAR library files required for your application.
You can learn more about working with JAR files in our tutorial: How to Work with Java JAR Files.
The contents outside this directory are not directly accessed by the client.
What is a Web Deployment Descriptor in Java?
As mentioned earlier, the web deployment descriptor (web.xml) file tells your container which servlet is going to handle a request from a given URL. To create the web.xml file, begin by creating the root element . In order to define a servlet and its mapping, you need a root element called .
There are two entries that the element takes in. The first entry is the name of the servlet and the second is the compile class or jsp file which matches this name.
After defining this, you need to define an element, which will map your to a given .
See the example below, which demonstrates how to create the and define the servlet and its mapping:
<web app>
<servlets>
<servlet name>web applicationservlet name>
<servlet class>com.developer.MyServletservlet class>
servlets>
<servlet mapping>
<servlet name>web applicationservlet name>
<url pattern>/webapi/*url pattern>
servlet mapping>
From the file above, when a user tries to access the sample link (http://localhost:8080/webapi/names), their request will be routed to the MyServlet instance.
How to Deploy a Web App in Java
After packaging all the files needed for your web app (using the standard directory structure), you need to deploy it on your server so that your user can access it on the Internet.
There are two methods of deployment: developers can either place the entire directory (MyWebApp/) in the application directory of the server or create a .war file and place it in this directory. The .war (Web Archive) file is a type of compressed file.
To deploy a web app In Tomcat, simply place MyWebApp/ in the webapps directory. The same goes for the .war file.
You can create a .war file from the directory of your web app using the command below:
$ jar cvf MyWebApp.war *
This will create a .war file in the current directory.
Final Thoughts on Creating a Dynamic Java Web App
This Java programming tutorial covered the steps needed to create a dynamic web application using the J2EE standard. You can learn more Java programming concepts by checking out our Java software development section.