How Tomcat works (iii) connector: Connector

Source: Internet
Author: User
As mentioned in the introduction, Catalina has two main modules: connectors and containers. In this chapter, you will write a connector that can create better request and response objects to improve the program in Chapter 2nd. A connector compliant with Servlet 2.3 and 2.4 specifications must create javax. servlet. http. httpservletrequest and javax. servlet. http. httpservletresponse and pass them to the service method of the called servlet. In the applications in this chapter, the connector parses the HTTP request header and allows the servlet to obtain the header, cookies, parameter names/values, and so on. You will improve the getwriter method of the response class in Chapter 2nd so that it can run correctly. With these improvements, you will get a complete response from primitiveservlet and be able to run more complex modernservlet.

The connector you created in this chapter is a simplified version of the default connector for Tomcat 4, which will be discussed in chapter 4th. Tomcat's default connector is not recommended in Tomcat 4, but it can still be used as a great learning tool. In the rest of this chapter, "ctor" refers to the modules built into our applications.
Note:: Unlike the application in the previous chapter, the connector and container are separated in the application in this chapter.

The applications in this chapter can be found in the ex03.pyrmont package and its sub-packages. These classes that make up the connector are part of the package ex03.pyront. connector and ex03.pyrmont. connector. HTTP. At the beginning of this chapter, each included program has a bootstrap class to start the application. However, there is no mechanism at this stage to stop this application. Once running, you must shut down the application by shutting down the console (Windows) or killing the process (Unix/Linux. Let's talk about the stringmanager class in org. Apache. Catalina. util. This class is used to process the internationalization of error messages of different modules and Catalina in this program. 1. stringmanager class: this class is used to implement international error prompts, because it is difficult to maintain only one configuration file in Tomcat, the practice here is that each package will have a configuration file, and each property file will be a Org. apache. catalina. util. processed by the stringmanager class instance. So when Tomcat runs, there will be multiple stringmanager instances. This class is designed as a singleton mode to avoid resource waste. Basic usage: stringmanager Sm = stringmanager. getmanager ("ex03.pyrmont. connector. HTTP "); Public String getstring (string key) 2. Application Description: This section contains three packages: Connector + startup + core. the startup module has only one class, bootstrap, which is used to start the application. The class of the connector module can be divided into five groups: the connector and its support Class (httpconnector and httpprocessor ). It refers to the HTTP request class (httprequest) and its auxiliary class. It refers to the HTTP response class (httpresponse) and its auxiliary class. Facade class (httprequestfacade and httpresponsefacade ). The constant class core module consists of two classes: servletprocessor and staticresourceprocessor. In Chapter 2nd, the httpserver class is separated into two classes: httpconnector and httpprocessor. The request is replaced by httprequest, and the response is replaced by httpresponse. Similarly, the application in this chapter uses more classes. 3. Start app Bootstrap: The main method in this class simply creates an httpconnector object and calls its connector. Start (); method. It is just an entry. Iv. connector module: 1. httpconnector: this class is started as a separate thread. It defines a serversocket and listens to port 8080. Then a blocking loop is provided. When a socket object is received, an httpprocessor object is created and its process method is executed. In general, httpconnector is only used to listen for requests, and the rest is completely handled by httpprocessor. 2. httpprocessor: the most important method for this class is public void process (Socket socket). A. In this method, first obtain the input and output streams of the socket, encapsulate it into a custom Stream object. B. Create an httprequest object that implements the httpservletrequest interface. The appearance object httprequestfacade. C is also used to create the httpresponse object, which implements the httpservletresponse interface. Similarly, the request line is parsed using the appearance object httpresponsefacade. D and private void parserequest (socketinputstream input, outputstream output. E. Private void parseheaders (socketinputstream input) parses the header to generate an httpheader object. F. Obtaining parameters will only parse the query string or POST request content when getparameter () is called for the first time, so as to improve the efficiency. G. Select servletprocessor or staticresourceprocessor based on whether "/servlet/" is included. 3. httpresponse: The biggest difference between this class and section 2 is that the getwriter () method is improved, and a custom writer is returned, which can be automatically refreshed.

How Tomcat works (iii) connector: Connector

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.