Tomcat 在默认情况下,如果需要访问页面的首页,需要以 IP + 工程名称 + 页面文件名称 的方式才能访问到,如果想要设置直接以 IP 的形式访问到首页,需要经过以下几个步骤
在 web.xml 中设置欢迎页:在<web-app>标签下设置如下内容,其中 index.jsp 即为首页文件
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
经过此步骤后,原来需要输入 http://192.168.1.100:8080/project/index.jsp 才可以访问首页,现在只需要访问 http://192.168.1.100:8080/project 就可以访问了。
设置 tomcat 目录下 conf 文件夹下的 server.xml 配置文件
<Host>
<Context path="" docBase="project"/>
</Host>
docBase的值设置为工程的名称,经过此步骤后,只需要输入 http://192.168.1.100:8080 就可以访问了。
设置 server.xml 中的默认端口:找到如下配置,将 port 属性由默认的 8080 修改为 80 就可以了。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
经过此步骤后,只需要访问 http://192.168.1.100 就可以成功访问到页面首页了。
示例 Server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<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" />
<GlobalNamingResources>
<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>
<Service name="Catalina">
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<!--初始化时的HOST路径-->
<!--
<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 "%r" %s %b" />
</Host>
-->
<!--以下HOST是使用IP直接访问的路径-->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="default"/>
</Host>
</Engine>
</Service>
</Server>