Steps to prepare First Web Application

Steps for Standalone (without using any IDE)

  1. Download and Install Server software[Tomcat].
  2. Prepare Web Application Directory Structure.
  3. Prepare the Web Resources
  4. Prepare Deployment Descriptor[web.xml] File and provide the respective Server side components configuration.
  5. Start Server and Deploy the web application.
  6. Access the Web Application.

Steps for using with IDE

If we use the IDEs like Eclipse, IntelliJ Idea,… then we have to use the following steps.

  1. Download and Install Server software[Tomcat].
  2. Open IDE and integrate Server to the IDE.
  3. Create a Web Project in the IDE.
  4. Provide the required web resources like Servlets, JSPs,…
  5. Execute the web application.
    • 1. Start Server
    • 2. Deploy the project into the Server.
    • 3. Generate endpoints to access the application.

Download and Install Server software[Tomcat]

  1. Download Tomcat from the Internet.
  2. Double Click on “apache-tomcat-10.1.20.exe” file.
  3. Click on the “Next” button.
  4. Click on the “I Agree” Button.
  5. Select “Host manager” and “Examples” checkboxes.
  6. Click on the “Next” button.
  7. Provide the following details

Server Shutdown Port : 1918[Any Random Number]
Http/1.1 Connector port : 1010
——–
User Name : raghu
Password  : raghu

  1. Click on the “Next” button.
  2. Provide JRE version: C:\Java\jdk-17
  3. Click on the “Next” button.
  4. Provide Installation location: C:\Tomcat 10.1
  5. Click on the “Install” button.
  6. Deselect “Run tomcat”, “Readme” check boxes. 
  7. Click on the “Finish” button.

To check whether the Tomcat Server installed Successfully or not we have to execute the “Tomcat10” application file available at “C:\Tomcat 10.1\bin”.

Use the following url in the client address bar

http://localhost:1010/

To change the port number after the installation we have to modify the port number in the file “C:\Tomcat 10.1\conf\server.xml” .

<Connector port="1010" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
  />

To change the Tomcat Username and password we have to use the file “C:\Tomcat 10.1\conf\tomcat-users.xml” .

<user username="raghu" password="raghu" roles="manager-gui,admin-gui" />

Prepare Web Application Directory Structure

To prepare web applications , we have to use the following directory Structure

When we install Tomcat Software in our system then the Tomcat Software will provide a folder like “Tomcat 10.1”.

Where tomcat 10.1 folder contains the following folders.

  1. bin: It includes .exe files, batch files to start server and to stop server.
  2. conf: It includes .xml files to provide server configurations, tomcat users details,…
  3. lib: It includes .jar files which are required by the Tomcat Server.
  4. logs: It includes .txt files or .log files to show the server history.
  5. work: It includes the duplicate applications of the web applications which are provided in webapps folder in order to perform internal activities of the server over the applications.
  6. temp: It includes all the temporary files which are generated during the application execution.
  7. webapps: It include all the web applications 

In Tomcat Server, if we want to prepare any web application then we have to create a separate application folder inside the webapps folder, where the application folder contains the following folders.

Application folder structure :

  1. images: It includes all the .jpg, .png files in order to provide background images, logos,…
  2. themes: It includes all the css files in order to improve look and feel.
  3. literature: It includes the .doc, .docx, .pdf files to representation documentation about the present web application.
  4. src: It includes all the .java files.
  5. WEB-INF: It includes the web application information like deployment descriptor, lib folder….

WEB-INF includes the following files and folders.

  1. web.xml : it is a deployment descriptor , it will provide the application configuration details.
  2. lib: It includes the application dependent jar files.
  3. classes: It included all the server side components .class files.  

The above web application directory structure is divided into the following two parts.

  1. Public Area / Client Area
  2. Private Area / Server Area

Public Area / Client Area: 

In web application directory Structure, inside the Application folder and outside of the WEB-INF folder is treated as public Area or Client Area 

If we provide any resource under the public area then the Clients can access that resource directly by using the resources names.

EX: In general, we will keep .html files, .jsp files under the application folder, to access those resources we will use the resource name directly in the url.

http://localhost:1010/loginapp/loginform.html
http://localhost:1010/regapp/registrationform.jsp

Private Area / Server Area:

The area which is available under WEB-INF including classes is treated as Private Area / Server Area.

If we keep any resource under the private area there we are unable to access that resources directly by using their names in the URL, if we want to access that private area resources we have to define alias names to the resources in web.xml file and through the alias names only we are abl to access that resources.

EX: If we prepare LoginServlet class under classes folder then we are unable to access that Servlet class by using its name, where we have to define alias names like login in the web.xml file and we have to use the generated alias name to access that resource.

http://localhost:1010/loginapp/login
http://localhost:1010/regapp/reg

Prepare the Web Resources

In web applications, we will use html files, jsp files, servlets, folders, etc…. as per the application requirement.

To prepare Servlets in web applications we have to use the following predefined library.

Q) What is Servlet and in how many ways are we able to prepare Servlet classes?

Servlet is a Server side program, by executing the Servlets Servers will generate response.

Servlet is an object maintained by the Container in order to perform a particular Server side action and it must implement jakarta.servlet.Servlet interface either directly or indirectly.

As per the predefined library provided by the Servlet API, there are three ways to prepare Servlet classes.

1. By Implementing Servlet interface:

public class MyServlet implements Servlet {
—-----
}

2. By Extending GenericServlet abstract class:

public class MyServlet extends GenericServlet {
—---
}

3. By extending HttpServlet abstract class:

public class MyServlet extends HttpServlet {
—---
}

Note: To compile Servlet classes we have to set the “classpath” environment variable to the location where servlet-pi.jar file exist.

set classpath=C:\Tomcat 10.1\lib\servlet-api.jar

Prepare Deployment Descriptor