Tomcat, start here

How do you get your Java code to run on the web, and when you’re starting out, the first thing you’re going to be exposed to are servlets and JSPS and Tomcat is a container for both of them, and it handles the dynamic web part for you

(1) Where do you come from and where do you go?

(1) Tomcat and its friends

JBoss: Redhat Redhat, supports all JavaEE rules, suitable for large projects, for a fee

Weblogic: Orcale, supporting all JavaEE rules, suitable for large projects, for a fee

Websphere: IBM, supporting all JavaEE rules, suitable for large projects, for a fee

Resin: Caucho, support all JavaEE rules, the application tends to be widespread

As for me, I am Tomcat, from the Apache foundation. I may be an open source and free web server, but there is no such thing as a free lunch. I only support a small amount of javaEE specifications (Servlet/Jsp, etc.), so I run a lot of small and medium-sized projects here

(2) What is Tomcat used for?

Be a container for servlets, etc

For example, an existing search engine, let’s think of it as a complete Web project, based on the HTTP protocol request and response, I enter some content query, and it receives the request, and it simply thinks that there is a Servlet (some Java code applied to the Web project) behind it processing the request. It will process and query your data through a series of methods, for example, if normal, query, there are illegal words do not query, return text warning, and finally show the corresponding interface to the user, and Tomcat as a container, where the Servlet and other things

Make your Web project accessible to you/others in a browser

In order to access our local Web project in the browser, we need a URL, such as http://localhost:8080/ (local), which is IP+ port number, and this open port has to do something for me. It needs to find some information that I configured in web.xml. Then I find my Servlet, implement the function I need, execute it, and return the result to the browser, so that I can use Java code with some pages, realize the dynamic interaction of data, so as to view the effect in the browser

(2) Configure Tomcat

(1) Startup and shutdown of Tomcat

Tomcat relies on the JDK to run, and it will find the JDK through the JAVA_HOME path. We have Java foundation, so this step can be ignored

Methods a

Bat and shutdown.bat files represent startup and shutdown respectively in the bin folder of our installation path

For example, start Tomcat: D:\develop\Java\apache-tomcat-8.5.40\bin\startup.bat on my computer

Way 2

If you want to make it more convenient, you can set these two as environment variables as well

Computer -> Right-click properties -> Advanced System Settings (left) -> Advanced -> Environment Variables

Create a system variable. Variable name: CATALINA_HOME Variable value: Tomcat installation path

Modify the path variable –> append: %CATALINA_HOME%\bin

This way we can start our Tomcat directly by typing startup or shudown in CMD

After Tomcat is started, a dialog box named Tomcat is displayed, in which log information is displayed, indicating that Tomcat is successfully started

If you enter http://localhost:8080 in the address box of the browser, the configuration is successful if the Tomcat page is displayed

As the server is locally located, our host name can be http://127.0.0.1:8080 or http://localhost:8080, with the port before the localhost name. The default tomcat port is 8080

(2) Change the port number (optional)

Tomcat installation path \conf\server. XML: find Connector port=”8080″ and change the value of 8080

If you change the port number to 80, you do not need to enter the port number when accessing the server. The default HTTP port number is 80, that is, http://localhost

(3) Tomcat directory structure

  • Bin: stores Tomcat startup and shutdown scripts, such as startup.bat and shutdown.bat

  • Conf: Stores various configuration files, such as context. XML, web. XML, and tomcat-users.xml

    • Server. XML: Configure setver information such as port number and host
    • Web. XML: Works with web. XML under each application WEB-INF to configure servlets, sessions, etc
    • Tomcat-users. XML: user name, password and related permissions
  • Lib: Stores the JAR packages required by Tomcat

  • Logs: log files

  • Temp: stores temporary files generated when Tomcat is running. After Tomcat is shut down, items in this directory can be deleted

  • Webapps: Each folder in the directory corresponds to one of our Web applications

    • In the Webapps directory, there is a ROOT directory, which is the default Web application. If the internal web application is placed in webapps/ROOT, the application name is not given in the URL path.
  • Work: The application under Webapps automatically generates files when it runs. After JSP is accessed, the corresponding server files and bytecode files are generated

(4) Configuring virtual Directories (External Applications)

Webapps: Each folder in the directory corresponds to one of our Web applications

By default, folders under Webapps can be automatically managed by Tomcat into one web site by one, but if we have too many sites, one: too much memory, two: too many files is not conducive to management, so we need to create a space outside, scatter our Web site to other drive letters

One (1) method

Find conf/server.xml in the Tomcat installation directory

<Host name="localhost" appBase="webapps"
      unpackWARs="true" autoDeploy="true">
      <! -- Add location -->
</Host>
Copy the code
Add code:<Context path="/URL access path" docBase="Absolute path for external applications"/>
Copy the code
For example:<Context path="/web001" docBase="F:/develop/web001"/>
Copy the code

Two (2) method

Go to the Conf /Catalina/ LoaclHost folder in the Tomcat installation directory and create a folder named Web project

<?xml version="1.0" encoding="UTF-8"? >
<Context 
	docBase="Absolute path for external applications" 
	reloadable="true">
</Context>
Copy the code

(5) set up the home page of the site

When you deploy a site on the network, the Index page will be used as the front page of a website. In other words, you only need to enter the domain name to jump to the page without entering the file name. How to set the front page of a web project on the local Tomcat server?

1, we create a folder web1 in webapps as one of our Web projects, and create two HTML documents in it, page1.html and page2.html

2. In the web1 directory, create a folder named web-INF and create a file named web. XML

You can copy the format from somewhere else, such as the web.xml file under webapps\ROOT\ web-INF, and add the following code

  <welcome-file-list>
    <welcome-file>Home file name</welcome-file>
  </welcome-file-list>
Copy the code

Such as:

<?xml version="1.0" encoding="UTF-8"? >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <welcome-file-list>
    <welcome-file>page2.html</welcome-file>
  </welcome-file-list>

</web-app>
Copy the code

So we don’t need to specify the resource file name to access, page2 has become the front page of our site

(6) How do external networks access local Tomcat-based Web applications

Tomcat + peanut shell

We developed a web project locally, and we deployed it on a local Tomcat server, but if you want someone to access our project from the Internet, help us with temporary testing, or make comments, you can solve this problem simply by using Tomcat + peanut shell

First of all, install and register the peanut shell software, create a new Intranet mapping, input the corresponding data

Let’s access it through the Internet

The end:

If there is any inadequacy in the content, or wrong place, welcome everyone to give me a message to put forward opinions, crab crab everyone! ^_^

If it helps you, follow me! (The series of articles will be updated in the public account as soon as possible)

We don’t know each other here, but we are working hard for our dreams

A adhere to push original Java technology public account: ideal more than two days