Servlet Introduction

What is a Servlet?

Java Servlet is a program that runs on a Web server or application server, acting as an intermediate layer between requests coming from Web browsers or other HTTP clients and the database or applications on the HTTP server.

With Servlet, you can collect user input from web page forms, present records from a database or other sources, and dynamically create web pages.

Java Servlet usually achieves the same effect as programs implemented using CGI (Common Gateway Interface). However, compared to CGI, Servlet has the following advantages:

  • Performance is significantly better.
  • Servlet executes within the address space of the Web server. This eliminates the need to create a separate process to handle each client request.
  • Servlet is platform-independent because it is written in Java.
  • The Java Security Manager on the server enforces a series of restrictions to protect resources on the server computer. Therefore, Servlet is reliable.
  • The full functionality of the Java class libraries is available to Servlet. It can interact with applets, databases, or other software through sockets and RMI mechanisms.

Servlet Architecture

The following diagram shows the position of Servlet in a Web application.

Servlet 架构

Servlet Tasks

Servlet performs the following main tasks:

  • Read the 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 the implicit HTTP request data sent by the client (browser). This includes cookies, media types, and compression formats that the browser can understand, etc.
  • Process the data and generate results. This process may require accessing a database, executing RMI or CORBA calls, calling Web services, or directly computing the corresponding response.
  • Send explicit data (i.e., documents) to the client (browser). The format of the document can be varied, 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 cache parameters, and other similar tasks.

Servlet Package

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 usingjavax.servletandjavax.servlet.httpthe package, which is a standard part of the Java Enterprise Edition, an extended version of the Java class libraries that supports large 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 respectively.

Java Servlet is created and compiled just like any other Java class. After you install the Servlet packages and add them to the Classpath on your computer, you can compile Servlet 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 so you can start using Servlet. So fasten your seatbelt and join us on the Servlet learning journey! We believe you will enjoy this tutorial.

Other Extensions