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
Client Request: A client (e.g., browser) sends an HTTP request to the web server.
Request Forwarded to Servlet: The web server passes the request to the Servlet container (like Tomcat).
Servlet Processing: Container calls the servlet's service() method. Based on the request type (GET, POST), it invokes doGet() or doPost().
Business Logic Execution: Servlet processes the request (e.g., reading form data, querying a database).
Response Generation: Servlet creates a response (usually HTML) using HttpServletResponse.
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:
Servlets
JSP (JavaServer Pages)
JSF (JavaServer Faces)
JDBC
Enterprise JavaBeans (EJB)
Web Services
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
Unpacked Form: Where each directory and file exists in the file system separately.
Packed Form (WAR file): All directories and files are compressed into a single archive (WAR file) for distribution and installation.
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.xml: Deployment descriptor for servlet configuration.
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 <web-app> tag controls this:
If set to true: The server skips all annotations and only reads configuration from web.xml.
If set to false or not set at all: The server will also look for annotations in your Java classes during startup.
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.
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:
In the Projects window, right-click the Source Packages directory.
Select New -> Servlet.
In the dialog box:
Set the Class Name to WelcomeServlet.
Choose a package name (e.g., com.example.servlet).
Click Finish.
π Image Fig.7 : Creating Servletπ Image Fig.8: Entering the class name and the packageπ Image 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
Right-click on the MyFirstServlet project in the Projects window.
Select Build to compile the project.
After a successful build, right-click the project again and select Run.