From the course: Java EE 8 Essential Training

Servlet API overview - Java EE Tutorial

From the course: Java EE 8 Essential Training

Start my 1-month free trial

Servlet API overview

- [Instructor] The Servlet API is an essential component in Java EE's web-based technologies that provides critical features for building web applications. Let's start by taking a look at the overarching concept of a servlet. Servlets run in a servlet container, which is a web or application server that provides network services for receiving requests and sending responses. All Java EE application servers support the servlet spec and contain a servlet container where servlets can be initialized. Once initialized, a servlet is available for processing requests, which are typically HTTP requests sent by the web browser. Requests are processed using a servlet interface method that corresponds to the HTTP method of the request and then dynamic content is generated and sent to the browser. Servlet logic handles a variety of application concerns, such as writing data to a database or generating dynamic content, like HTML, to return to the browser. The servlet interface contains methods that we implement for processing requests with HTTP methods, such as GET and POST. The Servlet API allows developers to easily interact with HTTP requests and responses by providing easy access and creation of headers, cookies, parameters, and body content. Servlets must be configured in the application, which can be done using an annotation-based approach introduced in Java EE 7. Prior to that, a web.xml file was used for initializing a servlet. Java EE 7 also introduced asynchronous servlet processing, which allows the servlet container to process resources more efficiently by releasing threads while long-running operations occur. The main benefit of the Servlet API is the abstraction it provides over low-level interfaces for networking and request parsing, making it easier for Java developers to build web applications. Here you see an example of a servlet in its simple implementation of the doGET and doPOST methods. You will notice these methods gain access to request data using the HTTP servlet request interface. The content returned is created using the writer object found on the HTTP servlet response interface. These are two critical interfaces to understand when working with servlets. Within the servlet specification, there are other useful interfaces that we can use. For example, the HttpSession interface manages information spanning multiple requests for a user. The Cookie interface is used to pass data to and from the browser with every request, allowing some persistent state to reside on the browser. The ServletContext allows developers to interact with the underlying servlet container. And finally, Filters allow for additional tasks to be performed for every request when it's received or when a response is returned for it. As we work through this lesson, We'll see how servlets can be used to build Java web applications and explore these interfaces in more detail.

Contents