JAVA SERVLETS – MASTER NOTES
Exam■Oriented Consolidated Notes (All Units Covered)
1. Introduction to Servlets
Servlets are Java programs that run on the server side inside a servlet container. They receive client
requests, process them using business logic, and generate dynamic responses which are sent back
to the client through a web server.
• Server-side technology for building dynamic web applications
• Platform independent (Java-based)
• More efficient and scalable than CGI
• Runs inside a Servlet Container (e.g., Apache Tomcat)
2. Servlet Architecture & Life Cycle
Servlet architecture follows a request–response model. The servlet container manages servlet
loading, instantiation, initialization, request handling, and destruction.
Servlet Life Cycle Phases:
• Loading of Servlet Class
• Servlet Instance Creation
• init() – called once for initialization
• service() – called for every request
• destroy() – cleanup before removal
3. CGI vs Servlets
• CGI creates a new process per request; Servlets use threads
• Servlets are faster and memory efficient
• Servlets support session tracking and cookies
• Servlets are portable; CGI is not
4. Servlet API Packages
• [Link] – protocol independent
• [Link] – HTTP specific
5. Core Interfaces and Classes
• Servlet – main interface
• GenericServlet – protocol independent abstract class
• HttpServlet – HTTP-based servlet
• ServletRequest & ServletResponse
• HttpServletRequest & HttpServletResponse
6. GenericServlet
GenericServlet is an abstract class that implements the Servlet interface. It is protocol independent
and requires overriding the service() method.
7. HttpServlet
• Extends GenericServlet
• Supports HTTP methods
• Common methods: doGet(), doPost(), doPut(), doDelete()
8. GET vs POST
• GET sends data via URL – visible and limited length
• POST sends data via request body – secure and no size limit
• GET used for search, filters; POST for login, forms, uploads
9. RequestDispatcher
• Used for server-side request forwarding and including
• forward() – transfers control without URL change
• include() – includes output of another resource
10. forward() vs sendRedirect()
• forward(): server-side, same request, faster
• sendRedirect(): client-side, new request, URL changes
11. Session Tracking Techniques
• HttpSession (recommended)
• Cookies
• URL Rewriting
• Hidden Form Fields
12. Cookies
Cookies are small pieces of data stored on the client side used for maintaining user state.
13. Deployment Descriptor & Annotations
• [Link] – XML based servlet configuration
• @WebServlet – annotation-based configuration (Servlet 3.0+)
• Annotations reduce boilerplate and improve readability
14. MVC Architecture
• Model – business logic and data
• View – presentation layer
• Controller – handles request flow
15. Multithreading in Servlets
• Servlets use multithreading for handling multiple requests
• Each request handled by a separate thread
• Improves performance and scalability
FINAL EXAM CHECKLIST
• Definition and advantages of Servlets
• Servlet architecture diagram & explanation
• Servlet Life Cycle methods
• Difference between Servlet and CGI
• Packages: [Link] & [Link]
• GenericServlet vs HttpServlet
• GET vs POST comparison table
• RequestDispatcher – forward vs include
• sendRedirect vs forward
• Session tracking techniques
• Cookies – methods and usage
• ServletConfig vs ServletContext
• [Link] vs @WebServlet
• MVC architecture explanation
• Multithreading concept in servlets