Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
Tomcat is an open-source software developed by the Apache Software Foundation (ASF) to implement Java Servlet and JavaServer Page (JSP) technologies.
Tomcat is a male cat. In o'reilly's animal book, cats have been used as the cover of another book. Therefore, the title of Tomcat's o'reilly book is Snow Leopard ):
The cat changes to snow leopard
Functions of Tomcat
The core of Tomcat isServlet Container. Servlet is a special type of Java object that works on the Web server and is used to analyze requests and generate corresponding responses ). Servlet container is used to start and call servlet objects. When an HTTP request is sent to the Web server, the servlet container searches for the corresponding servlet. If the servlet does not exist, the container needs to create an object. Servlet iner then transmits the request content to the servlet and sends the reply generated by the servlet back to the server responsible for communication.Program.
Request in blue and reply in red
For more information about HTTP and web servers, see forest HTTP.
Web ServerUse the HTTP protocol to communicate directly with the customer. Tomcat itself contains the functions of web servers. You can also use other Web servers, such as Apache servers, to provide more powerful web servers.
Tomcat also has an important feature:Process JSPFile. JSP can be seen as a high-level servlet, which will eventually be translated into servlet operations. We use the template language to write JSP files, which is similar to the PHP writing method. JSP files can directly contain HTML elements. Using JSP technology, we can separate the view from the servlet and let JSP take charge of the page rendering mode to better implement MVC (Model-View-control ). The Jasper module in Tomcat is responsible for JSP translation.
Install Tomcat
The installation of Tomcat is very simple. The following uses Linux/MAC as an example:
1. Go to the Apache Tomcat website to download Tomcat
2. decompress the downloaded file. The folder contains the following content:
The bin folder contains executable scripts:
Startup. Sh
Shutdown. Sh
It is used to start and stop Tomcat respectively. You can add the bin path to the environment variable path so that you can directly use these two scripts in the command line.
After starting tomcat, you can access the address localhost: 8080 in the browser to check whether Tomcat is started properly. 8080 is the default port of Tomcat. If everything goes well, you will see the following page:
Note that if you do not have Java Runtime on your computer, you need to install Java runtime to make Tomcat run smoothly.
Webapps
When we have developed a network app, such as a website test, we can put the appWebappsFolder. The webapps folder contains a special root folder, which contains the default network app. When we access the root directory of the server, such as localhost: 8080/index.html, The index.html file in the rootfolder is located.
(After Tomcat is installed, the root folder contains a GUI page for setting tomcat. If you directly change the root folder, it is best to back up the original root folder first .)
We create a new folder named testin websiteand Add the following index.html file:
<Html><Body><P>Hello world!</P></Body></Html>
Access localhost: 8080/test/index.html in the browser and you can see
The server can normally provide static/test/index.html files.
In addition to HTML files, the app folder should contain other files related to the app, such as JSP files, CSS files, JavaScript files, and Servlet files. class file, model-related. class files.
Create an index. jsp file in the test Folder:
< Html > < Body > < P > Hello world! </ P > <% = New Java. util. Date () %> </ Body > </ Html >
Visit the following page in the browser:
As you can see, in JSPCodeCalledJava. util. Date ()To display the current time. The JSP file is translated into a servlet, And the servlet generates an appropriate response and passes it to the client, that is, the browser. The process of translation and reply is automatically completed by Tomcat.
Change Port
Tomcat is configured using a series of XML files. For example, the port above is 8080. Now, to change the port listened to by Tomcat, you can find the connector tag and the port attribute of the tag in CONF/server. xml. The original property value is 8080. Change it to another value and restart tomcat.
Summary
Tomcat is a set of software that implements Servlet and JSP. Here is a brief introduction to Tomcat.
You can quickly create a tomcat host on your computer and try the above content.
Welcome to continue reading the "Java quick tutorial" SeriesArticle