Introduction to Servlet
What is a Servlet?
A Java Servlet is a program that runs on a web server or application server. It acts as an intermediary layer between requests from web browsers or other HTTP clients and databases or applications on an HTTP server.
Using Servlets, you can collect user input from web forms, display records from databases or other sources, and dynamically create web pages.
Java Servlets often achieve similar results to programs implemented using CGI (Common Gateway Interface). However, Servlets have several advantages over CGI:
- Better performance.
- Servlets execute within the address space of a web server, eliminating the need to create a separate process to handle each client request.
- Servlets are platform-independent since they are written in Java.
- Security constraints enforced by the Java security manager on the server protect resources on the server computer, making Servlets trustworthy.
- The full functionality of the Java class library is available to Servlets, allowing them to interact with applets, databases, or other software via sockets and RMI mechanisms.
Servlet Architecture
The diagram below shows the position of Servlets in a web application.
Servlet Tasks
Servlets perform the following main tasks:
- Reading explicit data sent by clients (browsers), including HTML forms from web pages or forms from applets or custom HTTP client programs.
- Reading implicit HTTP request data sent by clients, including cookies, media types, and compression formats understood by the browser.
- Processing data and generating results, which may involve accessing databases, performing RMI or CORBA calls, invoking web services, or directly computing responses.
- Sending explicit data (documents) to clients, which can be in various formats such as text files (HTML or XML), binary files (GIF images), Excel, etc.
- Sending implicit HTTP responses to clients, including specifying the type of document returned (e.g., HTML), setting cookies and cache parameters, and other related tasks.
Servlet Packages
Java Servlets are Java classes that run on web servers equipped with interpreters supporting the Java Servlet specification.
Servlets can be created using the javax.servlet and javax.servlet.http packages, which are standard components of the Java Enterprise Edition, an extended version of the Java class library supporting large development projects.
These classes implement the Java Servlet and JSP specifications. At the time of writing this tutorial, the respective versions are Java Servlet 2.5 and JSP 2.1.
Java Servlets, like any other Java classes, are created and compiled. After installing the Servlet packages and adding them to your computer's Classpath, you can compile Servlets using the JDK's Java compiler or any other compiler.
What's Next?
Next, this tutorial will guide you step-by-step in setting up your Servlet environment to start using Servlets. So, buckle up and join us on this journey to learn about Servlets! We believe you will enjoy this tutorial.