Servlet Web Application (original) (raw)

Last Updated : 31 Jul, 2025

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

Working of Servlets

  1. **Client Request: A client (e.g., browser) sends an HTTP request to the web server.
  2. **Request Forwarded to Servlet: The web server passes the request to the Servlet container (like Tomcat).
  3. **Servlet Processing: Container calls the servlet's service() method. Based on the request type (GET, POST), it invokes doGet() or doPost().
  4. **Business Logic Execution: Servlet processes the request (e.g., reading form data, querying a database).
  5. **Response Generation: Servlet creates a response (usually HTML) using HttpServletResponse.
  6. **Response Sent to Client: The Web server sends the generated response back to the client.

**What is a Web Application?

A Web application (Web app) is software accessed via a web browser over a network like the Internet or Intranet. While many web apps are built using languages like PHP or Perl, Java is widely used, especially for enterprise-level applications.

Java EE (Jakarta EE) offers components for building such apps, including:

Web Application Structure

A web application is organized into a hierarchy of directories and files. There are two ways to structure and deploy these files

Key Components of a Web Application

**1. Document Root: Top-level folder holding static content like HTML, JSP, CSS, JS and images.

**2. WEB-INF: Secure folder not accessible via browser; stores server-side resources.

**Web Application Deployment Descriptor (web.xml)

From Servlet API 3.0 onwards, the web.xml file can override any servlet annotations in your code. A special setting called metadata-complete in the tag controls this:

This gives developers more control over how their web app is set up and how fast it starts.

**Context Path

When a web application is deployed on a server, it’s assigned a context path, a unique name used in the URL.

For example, if the context path is /bookshop, accessing /bookshop/index.html retrieves the index.html file from the application's document root.

**Creating A Web Application Using NetBeans

Let’s create a simple HTML form that takes a user's name and submits it to a Servlet. The Servlet will respond with a personalized welcome message.

**Step 1. Create the Project in NetBeans

  1. Open NetBeans IDE.
  2. Go to File-> New Project.
  3. In the New Project dialog
  4. Select Java Web under Categories.
  5. Select Web Application under Projects.
  6. After selecting Web Application, click Next.

Fig.1: New Project dialog box

**In the Name and Location dialog:

Fig.2: Entering the name of the Web application

**In the Server and Settings step:

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

**Click Finish to create the MyFirstServlet application

Step 2. Create Form (index.jsp)

Let’s create a simple form to collect the user's name:

  1. In the Projects window, right-click the Web Pages directory.
  2. Choose New -> JSP from the context menu.
  3. In the dialog box, enter the filename as index.jsp and click Finish.

Fig. 6: Entering the file name

**index.jsp:

HTML `

1 <%@page contentType="text/htrnl" pageEncoding="UTF-8"%> 2 3

Smeta http-equiv="Content-Tyne" content="text/html; charset=UTF-8"> title>Welcome to MyFirstServlet example page

A Welcome Web Application

Name:

| input type="submit" value="Submit"/>

`

Step 3: Create the Servlet (WelcomeServlet.java)

Now that the HTML form submits the user's name to WelcomeServlet, let’s create this servlet to handle the request and respond with a personalized message.

**To create the servlet:

  1. In the Projects window, right-click the Source Packages directory.
  2. Select New -> Servlet.
  3. In the dialog box:
  4. Set the Class Name to WelcomeServlet.
  5. Choose a package name (e.g., com.example.servlet).
  6. Click Finish.

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

  1. Right-click on the MyFirstServlet project in the Projects window.
  2. Select Build to compile the project.
  3. After a successful build, right-click the project again and select Run.

Fig.10 : Build

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

Fig.11 : Running the project

**What Happens Next?

Fig.12: The application run in the web browser

Fig.13: Entering the name

Fig.14: Welcome message displayed