[Java advanced learning] Tomcat Web application deployment method and virtual host, IDEA configuration Tomcat

Server overview

Hardware server

oracle server (one unit worth millions of dollars)

  • Starting with 32G of memory, generally 128G
  • To learn and use, you can directly use the notebook as a hardware server (simulated hardware server: local computer)

Software server

Specifically: web application server, which is an application that can provide browser access

  1. Common categories of web application servers:
    • WebLogic: charges, currently the most widely used server
      • The server itself is very large, dozens of G
      • Generally used in large-scale projects: Jingdong, Taobao
    • WebSphere: Charges
      • The server is very large, dozens of G
    • Tomcat: Free
      • The size is very small, the installation program is only a dozen M
      • Tomcat is the most widely used web server in small and medium-sized enterprises/learning

Tomcat

  1. Operating environment:
    • Need the support of Java-jdk, you must install jdk first
    • Environment variables must be configured JAVA_HOME
  2. Correspondence with Java version
    • Tomcat6:jdk5.0
    • Tomcat7:jdk6.0
    • Tomcat8:jdk7.0
    • Tomcat9: the latest version, not recommended
  3. Tomcat7 startup: in a flash
    • JAVA_HOME is not configured
    • It has been started, it will not be able to be started when restarted/other programs occupy port 8080
  4. Solve the problem of not being able to start:
    • cmd: netstat -ano, check the PID of the process occupying port 8080, and end him
    • Change the Tomcat port number to something else, for example: 8090 (conf/server.xml–connector port), restart Tomcat after modification
  5. Tomcat's 80 port is a default port: when accessing Tomcat, you can directly enter localhost to access
  6. Tomcat directory structure
    • \bin stores the executable file for starting and shutting down Tomcat
    • \conf stores Tomcat configuration files
    • \lib stores the dependent jar packages
    • \logs store log files
    • \temp stores temporary files
    • \webapps store web applications (website code written by developers)
    • \work stores the JSP converted Servlet file (the real directory where Tomcat runs)
      • For example: .java files are run by Tomcat, and .class files are stored in the \work folder

basic concepts

  • Itself is a web application
  • tomcat is a servlet container
  • A Servlet container is a web container, and a web container is not necessarily a Servlet container.

3 ways to deploy web applications

Method 1: Restart the server to change

Create a web application: create a folder news1

  • news1: This folder is a web application
    • 1.html
    • 1.css
    • 1.js
    • WEB-INF folder
      • classes: compiled Java code (.class)
      • lib: store the jar packages needed by the project
      • web.xml: the core configuration file
  • deploy:

    • Add configuration in Tomcat/conf/server.xml

      In this way, do not write Chinese comments in the configuration file

      <Context docBase="D:\software\news1" path="/aaa"/>

      • docBase: Real path
      • path: The virtual path of the browser's visit
      <Host name="localhost"  appBase="webapps" 
            unpackWARs="true" autoDeploy="true">
      
          <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                 prefix="localhost_access_log." suffix=".txt"
                 pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      
          <Context docBase="D:\software\news1" path="/aaa"/>
      
    • Under the project path /WEB-INF/web.xmlin the configuration

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
      </web-app>
      
  • access:

    • Restart the Tomcat server
    • Enter in the address bar of the browser: localhost:8080/aaa/1.html----/aaa is the virtual path of the configured website access

Method 2: Deploy directly without restarting

  1. Create a web application: create a folder news2
    • news2: This folder is a web application
      • 1.html
      • 1.css
      • 1.js
      • WEB-INF folder
        • classes: compiled Java code (.class)
        • lib: store the jar packages needed by the project
        • web.xml: the core configuration file
  • deploy

    • web.xmlStill need to write under the web application directory

    • In the directory tomcat7\conf\Catalina\localhost, create anews2.xml

      • Tomcat will treat news2.xmlthe file name prefix as the virtual path of the current web application (localhost:8080/news2/1.html)
    • Edit the news2.xmlfile and add the actual path of the project

      <Context docBase="D:\software\news2" />
      
  • Configure the default ROOT.xml file

    • To news2.xmlrenameROOT.xml
    • Write directly in the address bar of the browser: localhost/1.htmlyou can access
    • priority:
      • Configured by yourself ROOT.xml---->Tomcatwabapps/ROOT/WEN-INF/ROOT.xml
  • access:

    1. Non-default ROOT.xml:localhost:8080/news2/1.html
    2. Default ROOT.xml:localhost:8080/1.html

Method 3: Automatic management

tomcat will automatically manage every web application in webapps

  • deploy:

    • Copy the web application to the tomcat/conf/webapps/next

    • Default mode

      • Change the project name under webaspps to ROOT directly
    • The default default access interface, directly input localhost, it will display the content of 1.html

      • Add configuration information to the ROOT/WEB-INF/web.xml file

        <welcome-file-list>
            <welcome-file>1.html</welcome-file>
        </welcome-file-list>
        
  • access:

    • Non-default: localhost:8080/news3/1.htmlcan be accessed through
    • Default: just by localhostvisiting

Configure virtual host

  1. In tomcat7/conf/server.xmlthe <Host name="localhost"></Host>Add Configuration contents<Host></Host>

    name: The domain name you want to modify

    appBase: the root path of the project (current directory structure D:\software\baidu\news3\ various files)

    <Host name="www.baidu.com" appBase="D:\software\baidu"></Host>
    
  2. Modify the local host file

    Host path:C:\Windows\System32\drivers\etc\Host

    The domain name access in the browser will be resolved through the DNS domain name resolver. If you do not modify the local localhost, you cannot access your virtual host; you can bypass DNS resolution by configuring the local Host file

    127.0.0.1       www.baidu.com
    
  3. test

    Browser address bar: www.baidu.com/news3/1.htmlOK

IDEA configure Tomcat

  1. Run—Configurations—+—Tomcat Server

Insert picture description here

  1. In the above interface, select Deployment—+ sign on the right to add the project's architecture
    Insert picture description here

  2. Start Tomcat, you can access the web project through the configured virtual path

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/114057868