一、tomcat端口配置
tomcat默认端口为8080,如果本地要启动多个tomcat怎么办呢?
1.找到tomcat下conf文件夹下的server.xml
<?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.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--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 (blocking & non-blocking)
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 that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- 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>
</Service>
</Server>
如果仅仅修改端口号,可仅修改
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
将port改为自己需要的端口,切记不要改为已经被占用的端口号。
而如果启动多个tomcat还需要修改
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
将port改为需要的,切记多个tomcat不要重复且端口号不要改为已经被占用的。
二、配置虚拟路径
好处:不用将web应用程序都放在tomcat的webapps目录中,可以存放在自定义的目录中。
两种常用方式:
1.在tomcat中conf/server.xml文件中Host元素内添加一个子元素Context,设置Context元素属性path和docBase,其中path为指定访问web应用程序的url路径,docBase为web应用程序存在于文件系统的真实路径,可以是绝对的,也可以是相对的,相对于Host元素的appBase的路径。
如添加
<Context path="/JavaWeb" docBase="E:\Project" />
其中path必须以/开头,其实是一个不存在的路径,所以为虚拟路径。配置好后在E盘创建Project文件夹并在Project文件夹下新建Test.jsp,启动tomcat测试,通过localhost:8080/JavaWeb/Test.html进行访问,便能看到Test.jsp中的内容了。
2.在conf目录下建立一个engineName([enginename]指的是Tomcat服务器使用的引擎名称,Tomcat使用的引擎是Catalina)目录,在其中建立hostName目录,在hostName中建立context.xml(此处随便命名,注意当启动tomcat后访问的一定要为自己命名的名字,如此处为context.xml访问地址为localhost:8080/context/要访问的文件)
<Context docBase="E:\Project" />
三、tomcat其他较为实用的配置
Context中有reloadable属性,该属性值为true时,在web应用程序的WEB-INF/classes目录下以及WEB-INF/lib目录中的class类文件发生改动,服务器会自动加载,该属性默认值为false,通常在开发阶段我们设为true,方便开发调试。在发布后记得一定要将改属性值改为false,提高tomcat性能。
<Context docBase="E:\Project" reloadable="true"/>
conf目录下还有个web.xml,其中有这样一段配置
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
将false改为true,这样url中访问的如果是目录,会在页面上列出目录下的文件。WEB-INF是隐藏的。注意开发时设为true方便浏览,发布后要改为false。
四、为什么访问时要用localhost或是127.0.0.1
因为conf/server.xml中配置Host中name值为localhost,同时hosts文件中将localhost与本机ip 127.0.0.1绑定在一起。
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
此处是可以修改的,如将name值改为test,还需要修改C:\Windows\System32\drivers\etc下面的hosts文件,添加
127.0.0.1 test
以后我们访问时就可以test:8080/。。。。。。进行访问了
五、java web项目目录结构
java web项目中因为包含内容非常多,所以一定要进行分类管理。下面为目录结构:
web应用程序
----WEB-INF(必须全大写)
----classes(存放类文件如servlet)
----lib(存放所需jar包)
----web.xml(存放当前web应用的配置信息文件)
对于jsp文件或者html文件,直接存放在目录的根目录下或者自定义目录分类存放。