What is Servlet? You will understand after reading it

Servlet provides a component-based and platform-independent method for creating web-based applications, which can be free from the performance limitations of CGI programs. Servlet has permission to access all Java APIs, including JDBC APIs that access enterprise-level databases.

This tutorial will explain how to use Java Servlet to develop web-based applications.

What is Servlet?

Java Servlet is a program running on a Web server or application server. It acts as an intermediate layer between a request from a Web browser or other HTTP client and a database or application on the HTTP server.

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

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

• Performance is significantly better.

• Servlet is executed in the address space of the Web server. In 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 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 Servlet. It can interact with applets, databases or other software through sockets and RMI mechanisms.

Servlet architecture

The following figure shows the location of the servlet in the web application.

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 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 compressed formats understood by the browser, etc.

• Process data and generate results. This process may need to access the database, perform RMI or CORBA calls, call Web services, or directly calculate the corresponding response.

• Send explicit data (ie documents) to the client (browser). The format of the document can be various, 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 the type of document to be returned (such as HTML), setting cookies and caching 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.

Servlet can be created using the javax.servlet and javax.servlet.http packages. It is a standard part of Java Enterprise Edition, which is 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 of the two are Java Servlet 2.5 and JSP 2.1.

Java Servlet has been created and compiled just like any other Java class. After you install the servlet packages and add them to the Classpath classpath on your computer, you can compile the servlet with the JDK's Java compiler or any other compiler.

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/112285017