Servlets and Tomcat

Source: Internet
Author: User

Web applications are not limited to static pages that appear on the server, but more dynamically generate page information based on requests, and can also extract data from the database and generate a page back to the user.

The first approach: Implement a server-side software following the HTTP protocol

The second approach: extend the functionality of the server-side software that implements the HTTP protocol to provide extensions based on the appropriate rules

The oldest Web server extension mechanism is CGI, which allows users to invoke CGI programs on a Web server.

The full name of the CGI Common Gateway Interface, the public gateway interface.

Most CGI programs are written in Perl and are written in C, Python, or PHP.

The process of user access to CGI includes:

    1. The user accesses the CGI program by clicking on a link or entering a URL directly into the browser;
    2. After the Web server receives the request, it discovers that the request is given to the CGI program, so it starts and runs the CGI program;
    3. The CGI program parses the CGI data in the request, processes the data and produces a response, which is usually an HTML page;
    4. This response is returned to the Web server, and the Web server wraps the response in the form of an HTTP response to the Web browser.

The disadvantages of CGI are:

Difficult to write CGI programs;

The response time of the user request is relatively long;

Performance is limited in a process manner.

Other CGI solutions:

Sun's servlet Technology (97), JSP Technology (98);

ASP Technology of Microsoft Corporation (96);

Rasmus Lerdorf invented PHP in 94.

Servlet and Servlet containers

Java Servlet (Java Server applet) is a Java-based Web component that runs on the server side and is managed by the Servelet container for generating dynamic content.

A servlet is a platform-independent Java class, and writing a servlet actually writes a Java class according to the specification of the servlet.

A servlet container, sometimes called a servlet engine, is part of a Web server or application that provides network services on top of sent requests and responses, decodes MIME-based requests, and formats MIME-based responses;

Servlets cannot run independently and must be deployed into a servlet container, and the methods of the servlet are instantiated and invoked by the container;

The servlet container embraces and manages servlets throughout the servlet's life cycle.

After the JSP is launched, the container that manages and runs the servlet/jsp is also called the Web container.

The process of user access to a servlet includes:

The user accesses the servlet by clicking on the link or entering a URL in the address bar;

After the Web server receives the request, it does not directly hand over the request to the servlet, but to the servlet container;

The servlet container instantiates the servlet, invokes a specific method of the servlet to process the request, and produces a response;

This response is returned by the servlet container to the Web server;

The Web server wraps the response and sends it to the Web browser in the form of an HTTP response.

There are advantages compared to Cgi,servlet:

The single-instance multithreaded operation of the servlet, where each request runs in a separate thread, while the servlet instance that provides the service has only one;

Servlets use threads rather than operating system processes;

The servlet uses the standard API and is supported by more Web servers;

Servlet is written in Java, with the advantages of easy development and platform independence;

Servlets can access the Java platform's rich class library;

The servlet container provides additional functionality to the servlet, such as error handling and security.

There are three types of servlet containers:

An independent servlet container;

The servlet container within the process;

Out-of-process servlet container.

Tomcat, like IIS, Apache and other Web servers, has the ability to handle HTML pages, and it is also a servlet and JSP container, and the standalone servlet container is the default mode for Tomcat.

However, Tomcat handles static HTML less than Apache, and we can combine them with Apache as an HTTP Web server, tomcat as a web container.

The process by which the Tomcat server accepts user requests and responds

The client (usually the browser) accesses the Web server and sends an HTTP request;

After the Web server receives the request, it is passed to the servlet container;

The servlet container loads the servlet and, after producing the servlet instance, passes it the object that represents the request and response;

The servlet instance uses the request object to obtain the client's request information and then handles it accordingly;

The servlet instance sends the processing results back to the client through the response object, which is responsible for ensuring that the response is sent out correctly while returning control to the Web server.

Tomcat directory structure and its purpose

Directory Use
/bin Storing scripts to start and close Tomcat
/conf Various configuration files for Tomcat, including Server.xml (primary configuration file)
/lib Store the jar files that the Tomcat server and Web application need to access
/logs Log file
/temp Temporary files
/webapps When publishing a Web application, place the Web application's directory and files in this directory
/work Tomcat places JSP-generated servlet source files and bytecode files in this

The Conf subdirectory under the Tomcat installation directory is used to store various configuration files for the Tomcat server, where the Server.xml is the main configuration file for Tomcat, a well-formed XML document in which the port number of the Tomcat default listener can be modified.

<connector port= "8080" protocol= "http/1.1"

connectiontimeout= "20000"

redirectport= "8443"/>

Tomcat Boot Analysis

The main analysis is the script under several bins

Tomcat Architecture ( not much to understand here, let's do it first )

Servlets and Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.