Skip to content
Related Articles
Open in App
Not now

Related Articles

Servlet – Web Application

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 06 Jun, 2022
Improve Article
Save Article

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver

Working With Servlets

Working with Servlets is an important step in the process of Application development and delivery through the Internet. A Servlet as explained earlier is a Java program that must be run by a Java Servlet engine on a Java-enabled Web server. The Servlet’s output is delivered to the Web browser. GlassFish Server Open Source 4.1 comes bundled with the NetBeans IDE 8.0.2 has built-in support for running Java Servlets. This book uses the bundled for the server to demonstrate and run Servlets.

What is a Web Application?

A Web application is sometimes called a Web app, Thus, it is an application that is accessed using a Web browser over the network such as the Internet or an Intranet. While many of the Web applications are written directly in PHP or Perl, Java remains a commonly used programming language for writing Web applications. This is especially true for Web-based enterprise applications as usually referred to as enterprise Web applications. Java EE provides several useful components such as JSP, JSF, Servlets, client-side applets, Enterprise JavaBeans, JDBC, and several other Web service technologies for writing enterprise Web applications. This chapter focuses on the writing Servlets as the Web application of choices.

Basically, a Web application is defined as a hierarchy of directories and files within a standard layout. There are two ways by which such a hierarchy can be accessed:

  • The first is where each directory and file exist in the file system separately. This is termed as the application in its unpacked form and used during the application development
  • The second is where all the subdirectories are zipped together in a packed form known as a Web ARchive or WAR file. It is used when distributing the applications to be installed

Organization Root Of A Web Application

Before beginning to work with the Servlets, it is useful to examine the runtime organizations of Web applications. Before the Servlet API Specification and version 2.2, across the servers, there was little consistency in the WAR file structure. However, Web servers that conform to the Servlet API version 2.2 [or later] the Web application ARchive must be in a standard format before it is accepted. These are discussed below.

Web Resources

In the Java EE architecture, Web components and static Web content files such as images are called Web resources. A web module is the smallest deployable and usable unit of Web resources. A Java EE Web module corresponds to the Web application as defined in the Java Servlet specification, And The top-level directory of A Web application hierarchy is also a document root of the application. Here, all the HTML files and JSP pages can be placed that comprise the application’s user interface.

WEB-INF: The Document root contains a subdirectory named /WEB-INF/. This subdirectory holds the following files and directories:

  • web.xml: Is the Web application deployment descriptor file
  • Classes: Is a directory that holds server side classes such as Servlets, utility classes JavaBeans components and so on

Deployment Descriptor

Servlet API version 3.0 onwards, the deployment descriptor of [web.xml] takes precedence over the annotations. This deployment descriptor overrides configuration information specified through the annotation mechanism. Version 3.0 of the web deployment descriptor contains a new attribute called metadata complete on the <web-app> element, which defines whether the web descriptor is complete or whether the class files of the web application should be examined for its annotations that specify the deployment information, and if this attribute is set to true, the deployment tool must be ignoring any servlet annotations are present in the class files and it is used only the configuration details mentioned in the descriptor. Otherwise, if the value is not specified or set to be false, the container must scan all classes files of the application for annotations. This will provide a way to enable or disable the scanning of the annotation and its processing during the startup of the application.

Context Path

When the application is being deployed on the particular Web server, Then a context path to the application has been assigned within it. Thus it means if the application is assigned to the context path as a bookshop, then a request of URI referring to /bookshop/index.html will retrieve the index.html file from that document root.

Creating A Web Application Using NetBeans

Let’s Create a simple HTML form that will accept the user’s name and submit the same to the current Servlet. The Servlet is in response to welcomes the user based on the user name it receives. To make the learning exercise simple, this chapter demonstrates Servlets using the NetBeans IDE. Refer to Appendix A: Installing NetBeans IDE to learn how to download, install and set up the IDE on the Windows Operating system. Use NetBeans to create a new Web Application Project called MyFirstServlet. Run the NetBeans IDE and selectFile New Project to create a new Web Application project in it. New Project dialog box Categories: Java JavaFX Jevo Web Java EE HTMLS Wed Application with E0sung Sources Web Free-Form Application lava ME Embedded java Cord Maven NetBeans Modules Select Java Web| Samples available under appears. option Categories list and Web Application option available under Projects list as shown in Fig.1

Fig.1: New Project dialog box

Click [NEXT] New Web Steps name and Location Application dialog box appears. Enter the name of the Web application as MyFirstServlet as shown in Fig.2 Click [NEXT] Server and Settings section of the New Web |Application dialog box appears. Keep the defaults as it is i.e. GlassFish Server 4.1 as the server, the Java EE 7 Web as the Java EE Version, and the /MyFirstServlet as Context Path as shown in diagram Fig.3

Fig.2: Entering the name of the Web application

Fig.3: Selecting the server, Java EE version, and the context path

Click Finish. The MyFirstServlet application is created in the NetBeans IDE as shown in Fig.4

Fig.4: MyFirstServlet in NetBeans IDE

Once the NetBeans IDE brings up the MyFirstServlet application, the next step is to create the HTML form and add the class [Servlet] file to the MyFirstServlet application.

By default the NetBeans IDE [as a part of web application creation makes available index.html. Delete this file, by selecting and right-clicking index.html in the Projects window and clicking Delete.

Creating index.jsp

Right-click Web Pages directory, select New -> JSP…, as shown in Fig.5

Fig.5: Clicking JSP…

New JSP dialog box appears. Enter index in the File Name textbox, as shown in Fig. 6 Click [FINISH]

index.jsp is created under the Web Pages directory.

Fig. 6: Entering the file name

By default, index.jsp holds the following code spec


Created on : 17 Feb, 2015, 10:17:42 AM
Author V5
%@page contentType-"text/html" pageEncoding="UTF-8"%>
<meta http-equiv"Content-Type" content="text/html; charset-UTF-8">
<title>JSP Page</title>
<h1>Hello World!</h1>

Now, modify index.jsp to hold the following code spec [HTML form:


1 <%@page contentType="text/htrnl" pageEncoding="UTF-8"%>
2 <!DOCTYPE html>
3 <html>
Smeta http-equiv="Content-Tyne" content="text/html; charset=UTF-8">
title>Welcome to MyFirstServlet example page</title>
<h1>A Welcome Web Application</h1>
  <form method="POST" action="WelcomeServlet">
<label for="name" title="Enter the name">Name: </label>
<input type="text" id="txtName" name="txtName"/><br><br>|
input type="submit" value="Submit"/>

Here, an HTML form has been added. This form accepts the user’s name using a text box and passes it to a Servlet called WelcomeServlet for further processing. We will be creating a servlet as shown below as follows ……

Fig.7 : Creating Servlet

Fig.8: Entering the class name and the package

Fig.9: Configuring the Servlet deployment

And After Creating the Servlet We Will Compiling And Building Using NetBeans And Runs the Web Application

Running The Web Application

  • Once the compilation and building of the Web application is done, run the application.
  • Right-click the MyFirstServlet project and select Build as shown in Fig.10

Fig.10 : Build

  • Once Run menu item is clicked, GlassFish Server 4.1 is started by the NetBeans IDE

Fig.11 : Running the project

  • index.jsp is served in the Web browser as shown in Fig.12

Fig.12: The application run in the web browser

  • Enter the name in the Name text box as shown in Fig.13. Click [Submit] Button Then,

Fig.13: Entering the name

WelcomeServlet is called, which displays the welcome message along with the name entered as shown in diagram Fig.14

  • Click here to return the home page to return to the main page ie. index.jsp.

Fig.14: Welcome message displayed

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!