大家好,今天分享tomcat 服务器
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器
下面再Windows 环境下,进行安装与配置
我使用的是这个版本:(下载下来之后,需要解压一下)
打开tomcat。可以看见里面的文件
bin: 这是运行它的脚本文件
conf : 这是tomcat 的配置文件
lib : 这里是它的jar包
logs : 这里是它的日志文件
webapps:这是存放网站的资源
work : 这里是它的工作文件
在这里提一下,Windows下的tomcat服务,解压之后就可以使用
但是可以会遇见启动时闪退的问题
点开这个文件
注意这两个文件
打开startup.bat (下面就是它的启动文件)
我们需要再这个startup,bat 这个文件当中加上(在这个文件的最前面)
SET JAVA_HOME= C:\Users\Administrator\.jdks\corretto-1.8.0_312(java jdk目录)
SET JRE_HOME=C:\Users\Administrator\.jdks\corretto-1.8.0_312\jre
SET CATALINA_HOME=E:\apache-tomcat-9.0.24-windows-x86\apache-tomcat-9.0.24 # 这是tomcat的安装目录(因各人而异)
截图是这样的
记得保存
打开shutdown.bat这个文件
在这个文件的第一行加上
SET JAVA_HOME= C:\Users\Administrator\.jdks\corretto-1.8.0_312
SET JRE_HOME=C:\Users\Administrator\.jdks\corretto-1.8.0_312\jre
SET CATALINA_HOME=E:\apache-tomcat-9.0.24-windows-x86\apache-tomcat-9.0.24
记得保存
点击startup.bat这个文件
可以dos窗口会有前台形式的运行进程(这是正常的)
如果我们刚刚不配置对应的环境变量,这个地方会闪退的
那样会导致启动失败
我们已经成功启动了tomcat 服务器
我们现在使用浏览器访问tomcat服务器 (http://localhost:8080/)
在我们进行网页访问的时候,startup.bat 文件的前台进程不可以断掉
否则浏览器的访问将出现问题:
这是关掉
再次运行:
这样就访问正常
也可以通过shutdown.bat 这个文件来关闭服务器
再次使用startup.bat 这个文件 来启动tomcat服务器
现在可以正常访问
这个文件是tomcat服务器非常重要的配置文件
这就是这个配置文件里所有的东西
<?xml version="1.0" encoding="UTF-8"?><!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
这是我们默认指定的端口
这一行要注意一下
这里有5个web应用,我们要注意ROOT 这个文件
这就是首页的网页资源
注意这个文件
这就是index.jsp 的全部代码:
<%–
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.
--%>
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title><%=request.getServletContext().getServerInfo() %></title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="navigation" class="curved container">
<span id="nav-home"><a href="${tomcatUrl}">Home</a></span>
<span id="nav-hosts"><a