0% found this document useful (0 votes)
3 views3 pages

Java Servlets: Architecture & Features

It is important
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Servlets: Architecture & Features

It is important
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Java Servlets

Java Servlet is a Java program that runs on a Java-enabled web server or


application server. It handles client requests, processes them and generates
responses dynamically. Servlets are the backbone of many server-side Java
applications due to their efficiency and scalability.

Features of Java Servlets

 Work on the server-side.


 Efficiently handle complex client requests.
 Generate dynamic responses.
 Provide better performance compared to older technologies like CGI.
 Highly scalable in enterprise-level web applications.

Java Servlets Architecture


Java servlets container play a very important role. It is responsible for
handling important tasks like load balancing, session management and
resource allocation, it make sure that all the requests are process efficiently
under high traffic. The container distribute requests across multiple
instances, which helps improve the system performance.
Servlet Architecture can be depicted from the image itself as provided below
as follows:
Servlet Architecture Workflow:
Execution of Servlets basically involves Six basic steps:
 The Clients send the request to the Web Server.
 The Web Server receives the request.
 The Web Server passes the request to the corresponding servlet.
 The Servlet processes the request and generates the response in the
form of output.
 The Servlet sends the response back to the webserver.
 The Web Server sends the response back to the client and the client
browser displays it on the screen.

Need of Server-Side Extensions

 Server-side extensions allow dynamic web page generation by running


programs on the server.
 Web servers provide APIs to help developers build these applications.
 Java Servlets are a key API for Java-based web development.

Servlet Container
Servlet container, also known as Servlet engine, is an integrated set of
objects that provide a run time environment for Java Servlet components. It
is a system that manages Java Servlet components on top of the Web server
to handle the Web client requests.

Services provided by the Servlet container:


 Network Services: Loads a Servlet class. The loading may be from a
local file system, a remote file system or other network services. The
Servlet container provides the network services over which the request
and response are sent.
 Decode and Encode MIME-based messages: Provides the service of
decoding and encoding MIME-based messages.
 Manage Servlet container: Manages the lifecycle of a Servlet.
 Resource management: Manages the static and dynamic resources, such
as HTML files, Servlets and JSP pages.
 Security Service: Handles authorization and authentication of resource
access.
 Session Management: Maintains a session by appending a session ID to
the URL path
Here's a simple example of how a servlet works:

Step 1: Create a Dynamic Web Project (in Eclipse)

 Open Eclipse -> File ->New ->Dynamic Web Project


 Name the project (e.g., HelloWorldServlet)
 Target runtime -> Select Apache Tomcat
 Click Finish

Step 2: Create Servlet class

 Right-click on src -> New-> Servlet


 Name it HelloWorldServlet and click Finish
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body><h1>Hello, World!</h1></body></html>");
}
}
Explanation:
 HelloWorldServlet extends HttpServlet.
 doGet() handles HTTP GET requests.
 [Link]("text/html") specifies response type.
 PrintWriter sends HTML response back to the client.

You might also like