Ajp 6th Chapter Self Notes
Ajp 6th Chapter Self Notes
1 Introduction to Servlets
Definition
• Servlets are Java programs that run on a server.
• They respond to client requests (e.g., via XHTML) and dynamically generate content
displayed in the browser.
• Managed by a servlet container (e.g., Tomcat), which is part of a web server.
Key Features
• Work primarily with HTTP (hence called "HTTP Servlets").
• Purpose: Enhance web server functionality by generating dynamic content.
How Servlets Work?
Step-by-Step Process
1. Client Request
o Client writes a URL request in a browser.
2. Web Server Interaction
o Web server locates the requested servlet.
3. Servlet Execution
o Servlet processes the request, gathers data, and generates a dynamic web
page.
4. Response
o Web page is sent back to the client browser.
Diagram (Working of Servlets)
Advantages of Servlets
1. Efficient Performance
o Run in the web server's address space, reducing overhead.
2. Platform Independence
o Can run on any server (Java-enabled).
3. Request-Response Model
o Handles HTML form inputs, communicates with the database, and applies
business logic.
4. Dynamic Content
o Servlets generate content dynamically (e.g., user logs, access times).
5. Multi-user Support
o Servlets synchronize multiple requests for better coordination.
6. Concurrency
o Handle concurrent requests efficiently.
❖ Methods in HttpServlet
- Request get handled with the help of below method
GET vs POST
• doGet: Requests data (less secure, more efficient).
• doPost: Submits data (more secure, less efficient).
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// handle GET request
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// handle POST request
}
Simple Servlet Program
6.1 Advantages
• The servlets are very efficient in their performance and get executed in the address
space of the belonging web server.
• The servlets are platform independent and can be executed on different web servers.
• The servlets working is based on Request-Response. Any HTML form can take the
user input and can forward this input to the servlet. The servlet can then be
responsible to communicate with the back-end database and manipulate the
required business logic. These servlets embedded on the web servers using Servlets
API.
• Servlets provide a way to generate the dynamic document. For instance: A servlet
can display the information of current user logged in, his logging time, his last access,
total number of access he made so far and so on.
• Multiple users can keep a co-ordination for some application among themselves
using servlets.
• Using servlets multiple requests can be synchronized and then can be concurrently
handled.
6.4 Servlet API Notes
The Servlet API provides the tools for building dynamic web applications. Two key
packages are used for servlet development:
1. javax.servlet: Contains core classes and interfaces for basic servlet functionalities.
2. javax.servlet.http: Contains additional classes and interfaces specifically for HTTP
servlets.
Method Purpose