Servlets 简明教程

Servlets - Overview

What are Servlets?

Java Servlet 是运行在 Web 或应用程序服务器上的程序,充当来自 Web 浏览器或其他 HTTP 客户端的请求与 HTTP 服务器上的数据库或应用程序之间的中间层。

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

使用 Servlet,您可以通过网页表单收集用户的输入,显示来自数据库或其他来源的记录,以及动态地创建网页。

Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Java Servlet 通常与使用通用网关接口 (CGI) 实现的程序具有相同用途。但与 CGI 相比,Servlet 提供了几个优点。

Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.

  1. Performance is significantly better.

  2. Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

  3. Servlets are platform-independent because they are written in Java.

  4. Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.

  5. The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.

Servlets Architecture

下图显示了 Servlet 在 Web 应用程序中的位置。

The following diagram shows the position of Servlets in a Web Application.

servlet arch

Servlets Tasks

Servlet 执行以下主要任务 -

Servlets perform the following major tasks −

  1. Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.

  2. Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.

  3. Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.

  4. Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.

  5. Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.

Servlets Packages

Java Servlet 是由 Web 服务器运行的 Java 类,该 Web 服务器具有支持 Java Servlet 规范的解释器。

Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification.

可以使用 javax.servletjavax.servlet.http 包创建 Servlet,这些包是 Java 企业版的标准部分,Java 类库的扩展版本支持大型开发项目。

Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java’s enterprise edition, an expanded version of the Java class library that supports large-scale development projects.

这些类实现了 Java Servlet 和 JSP 规范。在编写本教程时,版本是 Java Servlet 2.5 和 JSP 2.1。

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

Java servlet 的创建和编译与任何其他 Java 类一样。在安装 servlet 包并将它们添加到计算机的类路径后,可以使用 JDK 的 Java 编译器或任何其他当前编译器编译 servlet。

Java servlets have been created and compiled just like any other Java class. After you install the servlet packages and add them to your computer’s Classpath, you can compile servlets with the JDK’s Java compiler or any other current compiler.

What is Next?

我将一步步带您设置环境以开始使用 Servlet。因此,系好安全带,享受与 Servlet 的驾驶乐趣。我相信您会非常喜欢本教程。

I would take you step by step to set up your environment to start with Servlets. So fasten your belt for a nice drive with Servlets. I’m sure you are going to enjoy this tutorial very much.