Java Servlet Tutorial – Full Java Servlet Example

Java Servlet Tutorial series introduces about Java Servlet 3.0 Technology. With a tutorial, it also shows you how to create a Java Servlet Example project in simple step-by-step way. First, we are familiar to some concepts about general Java Servlet.

What is a Servlet?
A servlet is a Web component that is managed by a container and generates dynamic content. Servlets are Java classes that are compiled to byte code that can be loaded dynamically into and run by a Java technology-enabled Web server or Servlet container.

Java Servlet Tutorial - Java Servlet Example

Servlet Life Cycle
A servlet is managed through a well defined life cycle that defines how it is loaded and instantiated, is initialized, handles requests from clients, and is taken out of service. This life cycle is expressed in the API by the init, service, and destroy methods of the javax.servlet.Servlet interface that all servlets must implement
directly or indirectly through the GenericServlet or HttpServlet abstract classes. A servlet container also contains and manages servlets through their lifecycle

Servlet 3.0 is an update to the existing Servlet 2.5 specification. Servlet 3.0 required API of the Java Platform, Enterprise Edition 6. Servlet 3.0 is focussed on extensibility and web framework pluggability. Servlet 3.0 bring you up some extensions such as Ease of Development (EoD), Pluggability, Async Support and Security Enhancements

This tutorial is inspired from JSR-000315 Java Servlet 3.0 Final Release and just shows major aspects of Servlet 3.0. If you want to study more detail about Servlet 3.0 you can dig deeper into its specification.

Ease of Development

Here we discuss about the declarative style of programming through annotations in JEE6. Now, we can declare Servlets, Filter, Listeners, Init Params, and almost everything can be configured by using annotations. Several new annotations have been defined for ease of development in Servlet 3.0, allowing you to write a Servlet without requiring a descriptor and make the web.xml optional.

These below tutorials introduces you almost commonly Servlet 3.0 annotations and fully working example:

Servlet Annotation @WebServlet
Create a Servlet using annotation @WebServlet
Servlet 3.0 Annotation Example in Java
Moreover, you can refer to a way that create a servlet without using annotation
Java Servlet 3.0 Example using Web.xml

Servlet Annotation @WebInitParam
You can initialize parameter for servlet via annotation @WebInitParam
Servlet 3 Initialization Parameter Annotation @WebInitParam
You can also initialize parameter for servlet without using annotation
Servlet Context Parameter Example

Servlet Listener Annotation @WebListener
We can define a Listener using annotation @WebListener
Java Servlet Listener-ServletContextListener example

Servlet Filter Annotation @WebFilter
We can define a Filter using annotation @WebFilter
Filter Servlet 3 using Webfilter Annotation Example

Servlet Security Annotation @ServletSecurity
We can define security constraints using @ServletSecurity
Servlet Basic Authentication Annotation Example
We also configure security constraints without using annotation
Servlet Basic Authentication Web.xml Configuration

Servlet MultipartConfig Annotation @MultipartConfig
Instead of using third-party libraries like Commons file upload and COS multipart parser for handling file upload on web application. Now, With Servlet 3.0 we can define a servlet that handles multipart/form-data requests
Servlet 3 File Upload Example using MultipartConfig

You can create a sub-project or a module with a web-fragment.xml. It means that it allows to implement pluggable functional requirements independently.

Async Support
Servlet 3.0 provides the ability of asynchronous processing, for example: Waiting for a resource to become available, Generating response asynchronously.

Security Enhancements
Support for the authenticate, login and logout servlet security methods

The Java Servlet Tutorial shows you other Java Servlet examples like below:

Servlet HTTP Request Parameters Example
Servlet Request Session Application Scope Attributes
Java Servlet Forward HTTP Request Example
Servlet Redirect HTTP Request Example

This Java Servlet Tutorial will be updated by the time. Please visit it frequently to be up to date

1 thought on “Java Servlet Tutorial – Full Java Servlet Example”

Leave a Comment


Please share it if you found this useful
Hide Buttons