JavaEE简介

  • 概述

    • JavaEE(Java Enterprise Edition),Java企业版,是一个用于企业级web开发平台

    • 最早由Sun公司定制并发布,后由Oracle负责维护

    • JavaEE平台规范了在开发企业级web应用中的技术标准

      在这里插入图片描述

    • 在JavaEE平台共包含了13个技术规范(随着JavaEE版本的变化所包含的技术点的数量会有增多)

      • 它们分别是:JDBC、JNDI、EJB、RMI、Servlet、JSP、XML、JMS、Java IDL、JPA、JTA、JavaMail 和 JAF

      在这里插入图片描述

    • JavaEE版本变化

      年份 版本 对 JDK 的支持 Servlet 版本 JSP 版本
      1999年 12月 1.2 1.1 and later 2.2 1.1
      2001年 9月 1.3 1.3 and later 2.3 1.2
      2003年 11月 1.4 1.4 and later 2.4 2.0
      2006年 5月 5.0 5 and later 2.5 2.1
      2009年 10月 6.0 6 and later 3.0 2.2
      2013年 6月 7.0 7 and later 3.1 2.3
      2017年 8月 8.0 8 and later 4.0 2.3
    • JavaEE阶段需要学习的核心技能

      • Servlet、JSP、JSTL / EL、JavaBean、MVC模式、过滤器Filter、监听器listener、Ajax 分页
  • JavaEE应用服务器(JavaEE Application Server)

    • 概述

      • 应用服务器是Java EE规范的具体实现, 可以执行 / 驱动基于JavaEE平台开发的web项目
    • 常见的应用服务:

      • Weblogic(BEA Oracle 收费)

      • Webshpere(IBM 收费)

      • JBoss(RedHad 收费)

      • Resin(Caucho 收费)

      • JRun(Macromedia 收费)

  • Web容器(Web Server)

    • Geronimo(Apache 免费)

      • 只实现了JavaEE平台下部分技术标准,如Servlet,Jsp,JNDI,JavaMail
      • Web容器是开源免费的
    • Tomcat(Apache 开源免费)

    • Jetty(Jetty 开源免费)


Maven的安装和使用

  • Maven概述

    • Maven是什么?

      • Maven是专门用于管理和构建Java项目的工具
      • Maven全称Apache Maven,是一个(特别是Java编程)项目管理及自动构建工具,由Apache软件基金会所提供
      • 基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤,Maven能够很方便的帮你管理项目报告,生成站点,管理JAR文件
    • Maven的功能:

      • 提供一套标准化的项目结构
        • 让所有的 IDE1 使用 Maven 构建的项目结构完全一样,从而让 Maven 构建的项目有更高的移植性
      • 提供一套标准化的项目构建流程 (编译 → 测试 → 打包 → 发布)
        • 只需要敲一些简单的命令就能很快完成项目的创建,从而提高创建项目的效率
      • 提供一套依赖管理2机制
        • 只需要一个简单的配置就可以告别手动导入jar包了
    • Maven模型图:

      在这里插入图片描述
  • Maven仓库访问流程

    • 访问中央仓库

      在这里插入图片描述

      • 访问的具体过程
        • Java项目先到本地仓库中去找项目所需要引用的jar包,如果本地仓库没有该jar包,再去中央仓库中去找,找到就会直接将该jar包下载到本地仓库中,然后项目在进行引用
    • 访问远程仓库:(企业中常用的方式)

      在这里插入图片描述

      • 访问的具体过程
        • Java项目先到本地仓库中去找项目所需要引用的jar包,如果本地仓库没有该jar包,再去远程仓库中找,远程仓库没有再到中央仓库中找,找到以后就将该jar包下载到远程仓库和本地仓库,然后就可以再项目中引用了
        • 可能你有这样的疑惑这种方式看上去比比访问中央仓库要麻烦得多,为什么还要企业里面还常用
        • 中央仓库是在国外,我们国内访问会很慢(像访问GitHub一样,我们一般会使用国内的Gitee来代替),同时也存在一定的安全隐患(不能过渡依赖别人),而使用远程仓库就会很快,同时更安全,并且这种方式也不是很麻烦,远程仓库一般都会拷贝中央仓库中的所有jar包,并且还能导入公司自己开发的jar包
    • 当jar包被本地仓库引用后,以后再进行引用就不需要去下载了

  • Maven安装

    在这里插入图片描述

    • Maven安装后:

      在这里插入图片描述

    • 配置环境变量

      • 右击此电脑 → 高级系统设置 → 环境变量
      • 配置系统变量

        在这里插入图片描述

      • 配置Path路径

        在这里插入图片描述

    • 查询是否配置成功

      在这里插入图片描述

      • 备注:如果配置完 环境变量 后,显示mvn不是系统命令的,可以尝试一下重启电脑
    • 配置本地仓库

      • 进入Maven安装目录,然后进入conf文件夹,在进入settings.xml配置文件(推荐使用Notepad++打开)
        • 配置本地仓库:

        在这里插入图片描述

        • 配置案例远程仓库:

        在这里插入图片描述

        • 相关配置文件:
        <?xml version="1.0" encoding="UTF-8"?>
        <!--
        Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements.  See the NOTICE file
        distributed with this work for additional information
        regarding copyright ownership.  The ASF licenses this file
        to you under the Apache License, Version 2.0 (the
        "License"); you may not use this file except in compliance
        with the License.  You may obtain a copy of the License at
        
            http://www.apache.org/licenses/LICENSE-2.0
        
        Unless required by applicable law or agreed to in writing,
        software distributed under the License is distributed on an
        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
        KIND, either express or implied.  See the License for the
        specific language governing permissions and limitations
        under the License.
        -->
        
        <!--
        | This is the configuration file for Maven. It can be specified at two levels:
        |
        |  1. User Level. This settings.xml file provides configuration for a single user,
        |                 and is normally provided in ${user.home}/.m2/settings.xml.
        |
        |                 NOTE: This location can be overridden with the CLI option:
        |
        |                 -s /path/to/user/settings.xml
        |
        |  2. Global Level. This settings.xml file provides configuration for all Maven
        |                 users on a machine (assuming they're all using the same Maven
        |                 installation). It's normally provided in
        |                 ${maven.conf}/settings.xml.
        |
        |                 NOTE: This location can be overridden with the CLI option:
        |
        |                 -gs /path/to/global/settings.xml
        |
        | The sections in this sample file are intended to give you a running start at
        | getting the most out of your Maven installation. Where appropriate, the default
        | values (values used when the setting is not specified) are provided.
        |
        |-->
        <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
          <!-- localRepository
          | The path to the local repository maven will use to store artifacts.
          |
          | Default: ${user.home}/.m2/repository
          <localRepository>/path/to/local/repo</localRepository>
          -->
          <!--本地仓库路径-->
          <localRepository>D:\soft\安装包\Maven\apache-maven-3.6.1-bin\apache-maven-3.6.1\mvn_resp</localRepository>
          <!-- interactiveMode
          | This will determine whether maven prompts you when it needs input. If set to false,
          | maven will use a sensible default value, perhaps based on some other setting, for
          | the parameter in question.
          |
          | Default: true
          <interactiveMode>true</interactiveMode>
          -->
        
          <!-- offline
          | Determines whether maven should attempt to connect to the network when executing a build.
          | This will have an effect on artifact downloads, artifact deployment, and others.
          |
          | Default: false
          <offline>false</offline>
          -->
        
          <!-- pluginGroups
          | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
          | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
          | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
          |-->
          <pluginGroups>
            <!-- pluginGroup
            | Specifies a further group identifier to use for plugin lookup.
            <pluginGroup>com.your.plugins</pluginGroup>
            -->
          </pluginGroups>
        
          <!-- proxies
          | This is a list of proxies which can be used on this machine to connect to the network.
          | Unless otherwise specified (by system property or command-line switch), the first proxy
          | specification in this list marked as active will be used.
          |-->
          <proxies>
            <!-- proxy
            | Specification for one proxy, to be used in connecting to the network.
            |
            <proxy>
              <id>optional</id>
              <active>true</active>
              <protocol>http</protocol>
              <username>proxyuser</username>
              <password>proxypass</password>
              <host>proxy.host.net</host>
              <port>80</port>
              <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
            </proxy>
            -->
          </proxies>
        
          <!-- servers
          | This is a list of authentication profiles, keyed by the server-id used within the system.
          | Authentication profiles can be used whenever maven must make a connection to a remote server.
          |-->
          <servers>
            <!-- server
            | Specifies the authentication information to use when connecting to a particular server, identified by
            | a unique name within the system (referred to by the 'id' attribute below).
            |
            | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
            |       used together.
            |
            <server>
              <id>deploymentRepo</id>
              <username>repouser</username>
              <password>repopwd</password>
            </server>
            -->
        
            <!-- Another sample, using keys to authenticate.
            <server>
              <id>siteServer</id>
              <privateKey>/path/to/private/key</privateKey>
              <passphrase>optional; leave empty if not used.</passphrase>
            </server>
            -->
          </servers>
        
          <!-- mirrors
          | This is a list of mirrors to be used in downloading artifacts from remote repositories.
          |
          | It works like this: a POM may declare a repository to use in resolving certain artifacts.
          | However, this repository may have problems with heavy traffic at times, so people have mirrored
          | it to several places.
          |
          | That repository definition will have a unique id, so we can create a mirror reference for that
          | repository, to be used as an alternate download site. The mirror site will be the preferred
          | server for that repository.
          |-->
          <mirrors>
            <!-- mirror
            | Specifies a repository mirror site to use instead of a given repository. The repository that
            | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
            | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
            |
            <mirror>
              <id>mirrorId</id>
              <mirrorOf>repositoryId</mirrorOf>
              <name>Human Readable Name for this Mirror.</name>
              <url>http://my.repository.com/repo/path</url>
            </mirror>
            -->
          <!--阿里云远程仓库地址-->
          <mirror> <id>alimaven</id> 
          <name>aliyun maven</name> 
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
          <mirrorOf>central</mirrorOf> 
          </mirror>
          </mirrors>
        
          <!-- profiles
          | This is a list of profiles which can be activated in a variety of ways, and which can modify
          | the build process. Profiles provided in the settings.xml are intended to provide local machine-
          | specific paths and repository locations which allow the build to work in the local environment.
          |
          | For example, if you have an integration testing plugin - like cactus - that needs to know where
          | your Tomcat instance is installed, you can provide a variable here such that the variable is
          | dereferenced during the build process to configure the cactus plugin.
          |
          | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
          | section of this document (settings.xml) - will be discussed later. Another way essentially
          | relies on the detection of a system property, either matching a particular value for the property,
          | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
          | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
          | Finally, the list of active profiles can be specified directly from the command line.
          |
          | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
          |       repositories, plugin repositories, and free-form properties to be used as configuration
          |       variables for plugins in the POM.
          |
          |-->
          <profiles>
            <!-- profile
            | Specifies a set of introductions to the build process, to be activated using one or more of the
            | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
            | or the command line, profiles have to have an ID that is unique.
            |
            | An encouraged best practice for profile identification is to use a consistent naming convention
            | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
            | This will make it more intuitive to understand what the set of introduced profiles is attempting
            | to accomplish, particularly when you only have a list of profile id's for debug.
            |
            | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
            <profile>
              <id>jdk-1.4</id>
        
              <activation>
                <jdk>1.4</jdk>
              </activation>
        
              <repositories>
                <repository>
                  <id>jdk14</id>
                  <name>Repository for JDK 1.4 builds</name>
                  <url>http://www.myhost.com/maven/jdk14</url>
                  <layout>default</layout>
                  <snapshotPolicy>always</snapshotPolicy>
                </repository>
              </repositories>
            </profile>
            -->
        
            <!--
            | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
            | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
            | might hypothetically look like:
            |
            | ...
            | <plugin>
            |   <groupId>org.myco.myplugins</groupId>
            |   <artifactId>myplugin</artifactId>
            |
            |   <configuration>
            |     <tomcatLocation>${tomcatPath}</tomcatLocation>
            |   </configuration>
            | </plugin>
            | ...
            |
            | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
            |       anything, you could just leave off the <value/> inside the activation-property.
            |
            <profile>
              <id>env-dev</id>
        
              <activation>
                <property>
                  <name>target-env</name>
                  <value>dev</value>
                </property>
              </activation>
        
              <properties>
                <tomcatPath>/path/to/tomcat/instance</tomcatPath>
              </properties>
            </profile>
            -->
          </profiles>
        
          <!-- activeProfiles
          | List of profiles that are active for all builds.
          |
          <activeProfiles>
            <activeProfile>alwaysActiveProfile</activeProfile>
            <activeProfile>anotherAlwaysActiveProfile</activeProfile>
          </activeProfiles>
          -->
        </settings>
        
  • Maven常用命令

    • 代码展示

      mvn compile  //编译
      
      • 从阿里云(远程仓库)下载清理需要的插件jar包
      • 删除项目下的 target 目录
      mvn clean   //清理
      
      • 从阿里云(远程仓库)下载清理需要的插件的jar包
      • 在项目下会生成一个 target 目录
      mvn test  //测试
      
      • 从阿里云(远程仓库)下载测试需要的插件的jar包
      • 该命令会执行所有的测试代码
      mvn package   //打包
      
      • 从阿里云(远程仓库)下载打包需要的插件jar包
      • 在项目的 terget 目录下有一个jar包(将当前项目打成的jar包)
      mvn install   //安装
      
      • 从阿里云(远程仓库)下载安装需要的插件的jar包
      • 该命令会将当前项目打成jar包,并安装到本地仓库
  • powershell和cmd区别是什么

    • CMD 写的 BAT 脚本我们看作是面向过程的,而 PowerShell 则是面向对象的,是一种站在使用者的角度进行脚本的编写

    • CMD 只能执行基本的任务,PowerShell 因为是基于【.NET】面向对象的

  • Maven生命周期

    • 概念

      • 生命周期(Life Cycle)的概念应用很广泛,特别是在政治、经济、环境、技术、社会等诸多领域经常出现,其基本涵义可以通俗地理解为“从摇篮到坟墓”(Cradle-to-Grave)的整个过程
      • 在Maven中,生命周期就是用来描述Maven构建的项目所经历的的过程(就像人的儿童时期……老年时期),明确项目所处的阶段。Maven中常见的生命周期示意图:

      在这里插入图片描述

      阶段 处理 描述
      验证 validate 验证项目 验证项目是否正确且所有必须信息是可用的
      编译 compile 执行编译 源代码编译在此阶段完成
      测试 Test 测试 使用适当的单元测试框架(例如JUnit)运行测试
      包装 package 打包 创建JAR/WAR包如在 pom.xml 中定义提及的包
      检查 verify 检查 对集成测试的结果进行检查,以保证质量达标
      安装 install 安装 安装打包的项目到本地仓库,以供其他项目使用
      部署 deploy 部署 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程
    • Maven对项目构建的生命周期划分为3套:

      • clean
        • 清理工作

        在这里插入图片描述

      • default
        • 核心工作,例如编译,测试,打包,安装等

        在这里插入图片描述

      • site
        • 产生报告,发布站点等。这套声明周期一般不会使用

        在这里插入图片描述

    • 备注:

      • 同一套生命周期内,执行后边的命令,前面的所有命令会被自动执行
  • IDEA中Maven的使用

    • 知识点须知:Maven中的坐标

      • Maven中的坐标是资源的唯一标识
      • 使用坐标来定义项目或引入项目中需要的依赖
      • Maven 坐标主要组成:
        • groupId:
          • 定义当前Maven项目隶属组织名称(通常是域名反写,例如:com.hhxy)
        • artifactId
          • 定义当前Maven项目名称(通常是模块名称,例如 order-service、goods-service)
        • version
          • 定义当前项目版本号
        • scope
          • 定义jar包的作用范围
          • 常见有编译环境(compile,这是jar包默认的租用范围)、测试环境(test)、运行环境(runtime)
          依赖范围 编译classpath 测试classpath 运行classpath 例子
          compile Y Y Y logback
          test - Y - Junit
          provided Y Y - servlet-api
          runtime - Y Y jdbc驱动
          system Y Y - 存储在本地的jar包

          注: 表格中的 “Y” 代表该依赖在此阶段有效,“-” 代表无效

    • Maven项目的创建

      • 配置 Maven 环境 (File → Settings → Maven)
        • 集成Maven:

        在这里插入图片描述

        • 选择Maven的配置文件:

        在这里插入图片描述

        • 选择自定义的配置文件:

        在这里插入图片描述

      • 新建Maven项目(File → New → Module)

        在这里插入图片描述

        在这里插入图片描述

        • 创建成功后的Maven项目结构:
          在这里插入图片描述
    • Maven项目的导入:

      • 选择右侧Maven面板,点击 + 号

      在这里插入图片描述

      • 选中对应项目的pom.xml文件,双击即可

      在这里插入图片描述

      • MavenHelp插件的下载:(File → Settings → Plugins)

      在这里插入图片描述

      • 重启IDEA后生效,右击模块后效果如下:

      在这里插入图片描述

    • 使用Maven导入jar包

      • 使用Maven是直接通过设置pom.xml配置文件进行jar包导入,而不需要手动去网上搜索jar包然后进行导入,方便、快捷(也可以使用它来导入插件,方法是一样的)
        • pom.xml配置文件:
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
        
            <!--项目的坐标信息-->
            <groupId>com.hhxy.maven</groupId><!--项目组织名-->
            <artifactId>day2_maven_demo</artifactId><!--项目模块名-->
            <version>1.0-SNAPSHOT</version><!--项目版本号-->
        
            <!--JDK的版本-->
            <properties>
                <maven.compiler.source>8</maven.compiler.source>
                <maven.compiler.target>8</maven.compiler.target>
            </properties>
        
            <!--使用坐标导入jar包-->
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.27</version>
                </dependency>
            </dependencies>
        
        </project>
        
        • 备注:
          • 导入jar包的语句可以不写,直接去这个网址进行复制:Maven Repository: Search/Browse/Explore
          • 导入jar包的代码写好后要进行刷新操作(或者Ctrl+S也可以)
          • 也可以使用Alt+Insert快捷键,然后按Dependency进行导入(但是只能导入本地仓库中有的)
          • IDE(Integrated Development Environment )就是集成开发环境,常见的有:Eclipse、IDEA
          • 依赖管理就是管理项目中所依赖的第三方资源,比如jar包、插件……
          • 本地仓库(Local Repository):自己计算机上的一个文件夹
          • 远程仓库(Remote Repository):一般由公司团队搭建的私有仓库
          • 中央仓库(Central Repository):由Maven团队维护的全球唯一的仓库

Tomcat安装启动

  • 下载

    • 下载地址:http://tomcat.apache.org/

      在这里插入图片描述
  • 安装

    • tomcat由apache开源组织使用java开发的一款web容器, 在使用之前需要安装JDK及配置JAVA_HOME

    • Tomcat是绿色软解,解压就可使用

    • 如果之前已经安装了其他 tomcat 并且还配置了 CATALINA_HOME 不要忘记修改 CATALINA_HOME 指向我们现在使用的这个 tomcat

  • Tomcat启动

    • 运行startup.bat文件

      • 一定要配置 JAVA_HOME C:\Program Files\Java\jdk1.8.0_161
      • 部分电脑需要配置 CATALINA_HOME D:/***/***/apache-tomcat-9.0.41
      • 记住一个习惯: 以后我们装任何一个软件路径都应该避免中文,空格和特殊符号,可以使用_
  • Tomcat关闭

    • 运行shutdown.bat文件或者直接关闭掉启动窗口

  • 访问Tomcat

    • 访问Tomcat的URL格式:http://ip:port

    • 访问本机Tomcat的URL格式:http://localhost:8080


Tomcat结构介绍

  • Tomcat目录结构与介绍

    在这里插入图片描述

    • bin

      • bin目录主要是用来存放 tomcat 的命令文件,主要有两大类
        • 一类是以.sh结尾的(linux命令)
        • 另一类是以.bat结尾的(windows命令)
          在这里插入图片描述
    • conf

      • conf目录主要是用来存放tomcat的一些配置文件
        在这里插入图片描述
    • lib

      • lib目录主要用来存放tomcat运行需要加载的jar包
        在这里插入图片描述
    • logs

      • logs目录用来存放tomcat在运行过程中产生的日志文件
    • temp

      • temp目录用户存放tomcat在运行过程中产生的临时文件
        • (清空不会对tomcat运行带来影响)
    • webapps

      • webapps目录用来存放应用程序,当tomcat启动时会去加载webapps目录下的应用程序
        • 可以以文件夹、war包的形式发布应用
          在这里插入图片描述
    • work

      • work目录用来存放tomcat在运行时的编译后文件,例如JSP编译后的文件
        在这里插入图片描述

Tomcat部署项目

  • 方式一

    • 在Tomcat的webapps中创建一个我们自己的项目目录

      在这里插入图片描述

    • 在自己的myProject中放入一下静态资源

      在这里插入图片描述

    • 启动Tomcat, 访问对应的资源

      在这里插入图片描述

      在这里插入图片描述

    • URL的组成部分及作用

      在这里插入图片描述

      • 协议
        • http://
      • IP, 主机, 域名
        • 127.0.0.1/localhost 本机
      • 端口号
        • 8080 (Tomcat默认端口号)
      • 具体资源路径
        • myProject/index.html
      • 参数
  • 方式二

    • 项目不放在webapps中

      • localhost中添加配置文件
      • <Context path="/myProject" docBase="d:/myProject"/>
      • 建议配置文件名和项目名相同
        在这里插入图片描述

Tomcat常见配置

  • Tomcat配置文件介绍

    • Tomcat 的配置文件由4个xml组成,分别是 context.xml、web.xml、server.xml、tomcat-users.xml

    • 每个文件都有自己的功能与配置方法

  • context.xml

    • Context.xml 是 Tomcat 公用的环境配置

    • Tomcat 服务器会定时去扫描这个文件。一旦发现文件被修改(时间戳改变了),就会自动重新加载这个文件,而不需要重启服务器

  • web.xml

    • Web应用程序描述文件,都是关于是Web应用程序的配置文件

    • 所有Web应用的 web.xml 文件的父文件

  • server.xml

    • 是 tomcat 服务器的核心配置文件,server.xml 的每一个元素都对应了 tomcat 中的一个组件, 通过对 xml 中元素的配置,实现对 tomcat 中的各个组件和端口的配置

  • tomcat-users.xml

    • 配置访问Tomcat的用户以及角色的配置文件

  • 解决控制台乱码

    • 控制台产生乱码的原因是在Tomcat在输出日志中使用的是UTF-8编码,而我们中文的Windows操作系统使用的是GBK编码

    • 解决方式:

      • 修改 conf 目录中的 logging.properties 文件重新指定的编码方式
      • 如果还是不行, 那么就删除该行即可
      java.util.logging.ConsoleHandler.encoding = GBK 
      
  • 修改Tomcat监听端口

    • Tomcat 默认监听端口为 8080

    • 可以通过修改 server.xml 文件来改变 Tomcat 的监听端口

    <Connector port = "8080" protocol = "HTTP/1.1"
    connectionTimeout = "20000" 
    redirectPort = "8443"/> 
    // 80是http协议默认的端口号: 在http:1.1中, 如果不写端口号那么默认端口号就是80
    
  • 配置Tomcat并发数

    • Tomcat 的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能和 CPU 数量都有很大关系的,更好的硬件,更多的处理器都会使 Tomcat 支持更多的并发

    • 这个并发能力还与应用的逻辑密切相关,如果逻辑很复杂需要大量的计算,那并发能力势必会下降,如果每个请求都含有很多的数据库操作,那么对于数据库的性能也是非常高的

    • 对于单台数据库服务器来说,允许客户端的连接数量是有限制的,并发能力问题涉及整个系统架构和业务逻辑、系统环境不同、Tomcat版本不同、JDK版本不同、以及修改的设定参数不同,并发量的差异还是满大的,并发数设置参数有如下几个

    • maxThreads="1000"

      • 最大并发数
    • minSpareThreads="100"

      • 初始化时创建的线程数
    • maxSpareThreads="500"

      • 一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程
    • acceptCount="700"

      • 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理
      • 配置实例:
      <Connector port="8080" protocol="HTTP/1.1"
      minSpareThreads="100"  maxSpareThreads="500"
      maxThreads="1000"
      acceptCount="700" connectionTimeout="20000" redirectPort="8443"
      />
      
  • 配置Tomcat Manager

    • 什么是Tomcat Manager

      • Tomcat Manager是Tomcat自带的、用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用,默认情况下,Tomcat Manager是处于禁用状态的,准确的说,Tomcat Manager需要以用户角色进行登录并授权才能使用相应的功能,不过Tomcat并没有配置任何默认的用户,因此我们需要先进行用户配置后才能使用Tomcat Manager
    • 配置Tomcat Manager的访问用户

      • Tomcat Manager 中没有默认用户,我们需要在 tomcat-users.xml 文件配置
      • Tomcat Manager 的用户配置需要配置两个部分:角色配置、用户名及密码配置
    • Tomcat Manager 中的角色分类

      • manager-gui角色
        • 允许访问 HTML GUI 和状态页面 (即URL路径为/manager/html/*)
      • manager-script角色
        • 允许访问文本界面和状态页面 (即URL路径为/manager/text/*)
      • manager-jmx角色
        • 允许访问JMX代理和状态页面 (即URL路径为/manager/jmxproxy/*)
      • manager-status角色
        • 仅允许访问状态页面 (即URL路径为/manager/status/*)
  • 配置用户及角色

    <role rolename="manager-gui"/>
    <role rolename="manager-script"/> 
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
    <role rolename="admin-gui"/>
    <role rolename="admin-script"/>
    <user username="tomcat"   password="tomcat" 
    roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/> 
    

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐