Concocting a Java Web Application — Pt. I

Mihir Das
Mihir Das
Nov 3 · 6 min read

In this 4-part series, we will walk through creating a simple java web application.

What is a web application ?

Well, in simple terms, its a piece of software, or an application, that is deployed and is accessible via web pages, or APIs over the web. For an user, the user is accessing the functionalities provided by the application over the web, although the application itself is not running on the user system, and is run on the server.

Let’s throw in some technical jargon !!

  1. Webservice — Its a piece of functionality that is exposed over the web, or accessible over any network.
  2. API — API stands for Application Programming Interface. Think of it as an interface that dictates how other applications, or tools, or programs can access a piece of functionality. It can be done in various forms, use various protocols, but in the end all it means is an interface using which a program can access a certain piece of functionality.
  3. REST APIs — Whats all the buzz about REST ( REpresentational State Transfer ). Well, to present the gist of it, it is a way of letting a client access resources following a particular set of guidelines. All the data that can be given out are resources, and Rest gives the guidelines, a set of constraints per se, to be able to access the resources from a server.
  4. Java Web Application — As defined in the previous section, a web application is an application that is accessible to a client from the web. The web application can be a PHP application, a .NET application, a python application, a Ruby Application, a Node app, or any of the several popular languages available. All it means is that the server is able to provide a runtime for the type of application, and is able to server the requests of the user, and the server-side is coded in the given language which describes the application. And such is the case for JAVA Web Application. The server runs a java runtime, and has the application written in java, and is able to serve requests of the client.

Well, this much for starters !! We will visit more later, if the need comes.


In this series, we will be using a framework called Jersey, which is the reference implementation for Jax-rs, to build our java application.

What is Jax-rs ? And why the heck is Jersey even necessary ?

Jax-rs is the Java API for building restful services. But it provides just the API, or interfaces in terms of Java.

Java is always built around the motivation of Write Once Run Anywhere ( WORA ), and therefore to prevent vendor lock-in and give developers enough room to free develop using any third party library, java gives a set of guidelines, and the required interfaces. Its the third party’s job to provide the implementation of the interfaces, and the developers need to only implement the interfaces, thereby allowing the flexibility of the application to be used with any underlying third party application, that implements the given interfaces.

In this case, Java provides this framework of Jax-rs, which would be utilized by the application developer, to write Restful services. But nevertheless, Jax-rs provides us with an interface, and there should be some implementation that would be needed that does the underlying groundwork. And in this case the underlying implementation is given by Jersey.

Now that the essentials are all in place. Let’s download the requisites before going towards the building of the application.


I’ll be using an Eclipse IDE in this series, so you can go ahead with the same or use any other IDE of choice.

We need to download the Jersey libraries for the application to work. You can download most of the libraries from maven repositories. I am using 2.28 version in this project ( jaxrs-ri 2.28 ). You can download the latest version from the maven repository.

This completes the two prerequisites for building the application.


Creating the Webapp

  1. We will create a dynamic web project
Create a dynamic web project in eclipse

2. Give a name to the project

3. Let the default options stay for build output and src folders.

4. Check the deployment descriptor creation checkbox, and click Finish.

5. The final form in eclipse will be something as follows -

6. Creating src files for API calls.

Create the following two files in a package.

package main.apps.api;import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("test")
public class HelloWorldAPI {

@GET
public String getTest() {
return "Hello World !!";
}
}package main.apps.api;import javax.ws.rs.ApplicationPath;import org.glassfish.jersey.server.ResourceConfig;@ApplicationPath("service")
public class ApplicationConfig extends ResourceConfig{

public ApplicationConfig() {
packages("main.apps.api");
}
}

7. Once these two files are created, you might see some errors. At this point you need to go to the lib folder of the WEB-INF directory, created in your project, and copy the files from the jersey download. Copy all the jar files in the subfolders of the jersey zip, and paste them in the lib folder.

Clean, and build the project and this should resolve all errors.

8. Fill in the index.html , the homepage of our web application

<html><body>Hello World Generic Webapp</body></html>

9. Now we are in the final step, and now let’s fill in the deployment descriptor. This is a file, in this case web.xml, that would be present in the WEB-INF folder, and is necessary during the deployment of application on server. Server picks up a few configurations needed during the deployment of the application from this file.

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"><display-name>Generic Webapp</display-name></web-app>

What the heck was all that above ?

Let’s start with the two files.

Think of the file — ApplicationConfig, to be the starting point of all the API requests that come to your server, for this application. And the annotation @ApplicationPath does just that.

The packages function in the constructor basically tells the server that the class files present in the specified package are to be made accessible as URIs.

And at this point the actual webservice / REST API comes into play, in form of the file — HelloWorldAPI. Note the @Path annotation on the class, which instructs the server when the URI ( Uniform Resource Identifier , the URL which specifies which resource you want to access over the network ), has the route as test, and is coming to this application, the piece of functionality that would be accessed is coming from the class HelloWorldAPI.

Now we know which class to access for a certain URI. But what about the Http verbs, like GET, POST, PUT etc. This is then specified by the @GET annotation. When the proper URI is called by a client, or an application, and the type of request is GET, then it goes to the function annotated by GET. Note, that we can have another function with @POST, and that would be perfectly fine, for it would be used when in the same URI, a POST request is issued.

We will get to this later in more detail, in a later part of the series, where we discuss more on the jersey, and what are the things that we can do in a web application.

What lies ahead, matey !!

Up until now, we have seen how to make a web application in an IDE. In the next part we will look at setting up a server, deploying the application, and seeing it in action.

The source code is present in — Github Generic-Webapp , in case you are stuck at some point. It would also help you browse the project structure.

We will then go over jersey and jax-rs features in more details, and understand how the things operate under the hood.

Savvy’s Deck

All hands on deck, en-route to Savvyland ! Aye !!

Mihir Das

Written by

Mihir Das

Avid reader and a hobbyist poet, with a diet for technology

Savvy’s Deck

All hands on deck, en-route to Savvyland ! Aye !!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade