Into the Web – Part 1

It all started a few months back that I was trying to create a java application to track my daily activities and to learn some of the interesting java libraries and frameworks. But during the process I realized that instead of focusing on desktop based applications, it would be nice if I develop a small web application for that. And the best part is I never explored web development too much and it was challenging, because web development is something big “The technologies used in web development”. All I knew was a set of HTML tags and basic JavaScript and some other areas.

As a first step I started searching in the internet and some of the books about the web application development. Meanwhile during the searching process I came across Servlets “It is basically an executable which runs on the server side”. I learned about the basics of servlets, the related java classes and finally the request response programming model. Like every beginner I started with the “Hello World” program. So here I would like to tell you about my first experience “A servlet which return some HTML content on request.

Setting up the Server
Every web application require a server and to setup a server in my local I used the Appache Tomcat. Tomcat is simply an open source Java servlet container. Download the latest version of the Tomcat server from http://tomcat.apache.org/.

  1. Install the latest stable version of tomcat server
  2. Create a project folder “C:\projects\” and extract the downloaded file here
  3. Rename the folder “apache-tomcat-8.5.**” to “tomcat”
  4. Now we have our tomcat home ready and we will refer directory “C:\projects\tomcat” as tomcat_home

Starting and Stopping the Server
Starting and stopping the tomcat server is easy. We do have two executable files under “tomcat_home\bin\” directory. To start we do use “startup.bat” and to stop we do use “shutdown.bat”
After starting the server we can test this by opening a browser session and typing in “localhost:8080/”. If we are getting a tomcat home page then you are done, the server is up and running. πŸ™‚

Getting started with Web Application Development
Our web application require a root directory and we have to place this root directory under “tomcat_home\webapps\” directory. Let me tell you the steps one by one.

  1. Create a root directory called “helloworld” under “tomcat_home\webapps\” directory.
  2. Now create a new directory called “WEB-INF” under this root directory. We will store the web descriptor file under this directory
  3. Create a new directory called “classes” under the “WEB-INF” directory to store our servlet classes

Now create a new web deployment descriptor file named “web.xml”. The deployment descriptor works like a configuration file and it describe how the application or components should be deployed. The default content of the “web.xml” is given below.<!– Note: All elements MUST be grouped together and placed IN FRONT of the elements –>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <!-- To save as "tomcat_home\webapps\helloworld\WEB-INF\web.xml" --> 
 <!-- Note: All <servlet> elements MUST be grouped together and placed IN FRONT of the <servlet-mapping> elements --> 
</web-app>

Okay, now we need to see if the setup is correct or not by restarting the server. If we are getting the below given messages in the console then we are good and our application is deployed successfully πŸ™‚

29-Sep-2017 21:26:43.902 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Projects\tomcat\webapps\helloworld]
29-Sep-2017 21:26:43.933 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Projects\tomcat\webapps\helloworld] has finished in [30] ms

Accessing our Web Application
Once the server is up and running we can access the web application by issuing URL “localhost:8080/helloword/” where “8080” is the default port number. We can change this default port number by editing the “server.xml” file available under “tomcat_home\conf\” directory.

Now create a new HTML file “index.html” and save it under “tomcat_home\webapps\helloworld\” directory. In this HTML file we will write HTML tags that are required to display “Hello World” message. The structure of “index.html” is given below.

<html>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

After saving this file under application root directory which is “tomcat_home\webapps\helloworld”, open a new browser session and issue the below given URL

localhost:8080/helloworld/

Actually this one is a directory request and if out root directory have “index.html”, the server will redirect the request to “index.html” and the browser will display “Hello World!” message. πŸ™‚ You can use the below given URL too.

localhost:8080/helloworld/index.html

That is all for today. We will see how to write a servlet class and use it to process a request and send a response in the next part of “Into the Web”.

Advertisements