What is Servlet
A servlet is a Java program that runs in a Web server, as opposed to an applet that runs in a client browser. Typically, the servlet takes an HTTP request from a browser, generates dynamic content (such as by querying a database), and provides an HTTP response back to the browser. Alternatively, the servlet can be accessed directly from another application component or send its output to another component. Most servlets generate HTML text, but a servlet may instead generate XML to encapsulate data.
More specifically, a servlet runs in a J2EE application server. Servlets are one of the main application component types of a J2EE application, along with JavaServer Pages (JSP) and EJB modules, which are also server-side J2EE component types.
Prior to servlets, Common Gateway Interface (CGI) technology was used for dynamic content, with CGI programs being written in languages such as Perl and being called by a Web application through the Web server. CGI ultimately proved less than ideal, however, due to its architecture and scalability limitations.
Servlet containers, sometimes referred to as servlet engines, execute and manage servlets. The servlet container calls servlet methods and provides services that the servlet needs while executing. A servlet container is usually written in Java and is either part of a Web server (if the Web server is also written in Java) or is otherwise associated with and used by a Web server.
- javax.servlet.Servlet -> javax.servlet.GenericServlet -> javax.servlet.http.HttpServlet
What is filter
A Servlet filter is an object that can intercept HTTP requests targeted at your web application.
The order in which filters are invoked depends on the order in which they are configured in the
web.xml file. The first filter in
web.xml is the first one invoked during the request, and the last filter in
web.xml is the first one invoked during the response. Note the reverse order during the response.
What is event listener
The servlet specification includes the capability to track key events in your Web applications through event listeners. This functionality allows more efficient resource management and automated processing based on event status.
The event classes are as follows:
Event Listener Categories and Interfaces
|Event Category||Event Descriptions||Java Interface|
|Servlet context lifecycle changes||Servlet context creation, at which point the first request can be serviced
Imminent shutdown of the servlet context
|Servlet context attribute changes||Addition of servlet context attributes
Removal of servlet context attributes
Replacement of servlet context attributes
|Session lifecycle changes||Session creation
|Session attribute changes||Addition of session attributes
Removal of session attributes
Replacement of session attributes