Home>Article>Java> What is a servlet

What is a servlet

青灯夜游
青灯夜游 Original
2023-01-28 09:51:11 12792browse

The full name of Servlet is "Java Servlet", which means small service program or service connector in Chinese. It is a program running on a Web server or application server. It serves as a request and response from a Web browser or other HTTP client. A middle layer between databases or applications on an HTTP server. Servlet has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content.

What is a servlet

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

What is a servlet

Servlet (Server Applet) is the abbreviation of Java Servlet, called a small service program or service connector, using Java The server-side program written has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content.

Java Servlet is a program that runs on a Web server or application server and acts as an intermediary layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server.

In the narrow sense, Servlet refers to an interface implemented by the Java language. In the broad sense, Servlet refers to any class that implements this Servlet interface. Generally, people understand Servlet as the latter. Servlets run in Java-enabled application servers. In principle, Servlets can respond to any type of request, but in most cases Servlets are only used to extend Web servers based on the HTTP protocol.

The first to support the Servlet standard was JavaSoft's Java Web Server. Since then, some other Java-based Web servers have begun to support standard Servlets.

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

Java Servlet can usually achieve the same effect as a program implemented using CGI (Common Gateway Interface, public gateway interface). But compared to CGI, Servlet has the following advantages:

  • Performance is significantly better.

  • Servlet executes within the address space of the web server. This way it eliminates the 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 series of restrictions to protect resources on the server computer. Therefore, the Servlet is trusted.

  • All functions 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 diagram shows the location of Servlets in a web application.

What is a servlet

##Servlet Tasks

Servlet performs the following main tasks:

  • Read the explicit data sent by the client (browser). This includes HTML forms on a web page, or it can be a form from an applet or custom HTTP client program.

  • Read the implicit HTTP request data sent by the client (browser). This includes things like cookies, media types and compression formats that the browser understands.

  • Process data and generate results. This process may require accessing a database, performing an RMI or CORBA call, calling a Web service, or directly calculating the corresponding response.

  • Send explicit data (i.e. document) 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 an implicit HTTP response to the client (browser). This includes telling the browser or other client what type of document to be returned (such as HTML), setting cookies and cache parameters, and other similar tasks.

Servlet package

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

Servlets can be created using the javax.servlet and javax.servlet.http packages and are 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 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 packages and add them to the Classpath on your computer, you can compile the Servlet with the JDK's Java compiler or any other compiler.

For more programming-related knowledge, please visit:Programming Teaching! !

The above is the detailed content of What is a servlet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn