English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Servlet Introduction

What is Servlet?

Java Servlet is a program running on a Web server or application server, which acts as an intermediary layer between requests from Web browsers or other HTTP clients and databases or applications on the HTTP server.

With Servlets, you can collect user input from web forms, present records from databases or other sources, and dynamically create web pages.

Java Servlets are usually as effective as programs implemented using CGI (Common Gateway Interface, Public Gateway Interface), but Servlets have the following advantages over CGI:

  • Performance is significantly better.

  • Servlets execute within the address space of the Web server. This means that it is not necessary 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 executes a series of restrictions to protect the resources on the server computer. Therefore, Servlets are trusted.

  • All the functionalities of the Java class library are available to Servlets. It can interact with applets, databases, or other software through sockets and RMI mechanisms.

Servlet architecture

The following figure shows the position of Servlet in the Web application.

Servlet tasks

Servlet performs the following main tasks:

  • Read explicit data sent by the client (browser). This includes HTML forms on web pages, or it can also be 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 that the browser can understand, etc.

  • Process data and generate results. This process may require accessing a database, executing RMI or CORBA calls, invoking web services, or directly calculating the corresponding response.

  • Send explicit data (i.e., documents) to the client (browser). The format of the document can be diverse, including text files (HTML or XML), binary files (GIF images), Excel, and so on.

  • send an implicit HTTP response to the client (browser). This includes telling the browser or other client the type of document being returned (e.g., HTML), setting cookies and cache parameters, and other similar tasks.

Servlet Packages

Java Servlet is a Java class that runs on a web server with an interpreter that supports the Java Servlet specification.

Servlets can be used javax.servlet and javax.servlet.http package creation, which is a standard component of Java Enterprise Edition, an extended version of the Java class library that supports large-scale development projects.

These classes implement the Java Servlet and JSP specifications. At the time of writing this tutorial, the corresponding versions are Java Servlet 2.5 and JSP 2.1.

Java Servlets are created and compiled just like any other Java class. After you install the Servlet package and add them to your computer's Classpath, you can compile Servlets using the Java compiler of JDK or any other compiler.

What's next?

Next, this tutorial will guide you step by step to set up your Servlet environment, so that you can start using Servlets. Therefore, please fasten your seatbelt and join us on the journey of learning Servlets! We believe you will enjoy this tutorial.