Servlet Introduction
What is a Servlet?
A Java Servlet is a program that runs on a Web server or application server. It acts as a middle layer between requests from Web browsers or other HTTP clients and databases or applications on the HTTP server.
Using Servlets, you can collect user input from web forms, present records from databases or other sources, and create web pages dynamically.
Java Servlets can usually achieve the same effect as programs implemented with CGI (Common Gateway Interface). However, compared with CGI, Servlets have the following advantages:
- Significantly better performance.
- Servlets execute within the address space of the Web server. This way, there is no need to create a separate process to handle each client request.
- Servlets are platform-independent because they are written in Java.
- The Java security manager on the server enforces a set of restrictions to protect resources on the server computer. Therefore, Servlets are trustworthy.
- The full functionality of the Java class library is available to Servlets. They can interact with applets, databases, or other software through sockets and RMI mechanisms.
Servlet Architecture
The following diagram shows the position of Servlets in a Web application.
Servlet Tasks
Servlets perform the following main tasks:
- Read explicit data sent by the client (browser). This includes HTML forms on web pages, or forms from applets or custom HTTP client programs.
- Read implicit HTTP request data sent by the client (browser). This includes cookies, media types, and compression formats understood by the browser, and so on.
- Process the data and generate results. This process may require accessing a database, making RMI or CORBA calls, invoking Web services, or directly computing the corresponding response.
- Send explicit data (i.e. documents) to the client (browser). The document can be in a variety of formats, including text files (HTML or XML), binary files (GIF images), Excel, etc.
- Send implicit HTTP responses to the client (browser). This includes telling the browser or other clients the type of document being returned (e.g. HTML), setting cookies and caching parameters, and other similar tasks.
Servlet Packages
A Java Servlet is a Java class that runs on a web server with an interpreter that supports the Java Servlet specification.
Servlets can be created using the javax.servlet and javax.servlet.http package, which is a standard part of Java Enterprise Edition, an extended version of the Java class library that supports large development projects.
These classes implement the Java Servlet and JSP specifications. At the time of writing this tutorial, the corresponding versions were Java Servlet 2.5 and JSP 2.1.
Java Servlets are created and compiled like any other Java class. After you install the Servlet packages and add them to the Classpath on your computer, you can compile Servlets with the JDK's Java compiler or any other compiler.
What's Next?
Next, this tutorial will guide you step by step through setting up your Servlet environment to begin using Servlets. So fasten your seat belts and join us on this journey of learning Servlets! We believe you will enjoy this tutorial.