【问题标题】:Deployed war file on tomcat gives 404 error在tomcat上部署war文件会出现404错误
【发布时间】:2023-03-23 05:48:01
【问题描述】:

我已使用 ant build.xml 文件成功地将我的小型 Web 应用程序的 war 文件部署到我的本地 tomcat 7.0.42 服务器。当我在我的应用程序中输入网址localhost:8080/springapp/index.jsp 时,我收到 404 错误。我不明白为什么在部署 war 文件时出现错误。

我知道我的部署是成功的,因为我可以看到在 tomcat 管理器应用程序localhost:8080/manager 中列出的/springapp。我一直在关注本教程http://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html,除了更改一些内容之外几乎完全一样,因为我使用的是 tomcat 7,而它使用的是 tomcat 6。我目前正在执行步骤 1.4。 任何帮助将不胜感激,我在谷歌搜索和故障排除一天多的时间里一直被困在这个问题上。

这是我的 build.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project name="springapp" basedir="." default="usage">
    <property file="build.properties"/>

    <property name="src.dir" value="src"/>
    <property name="web.dir" value="war"/>
    <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
    <property name="name" value="springapp"/>

    <path id="master-classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
        <!-- We need the servlet API classes: -->
        <!--  * for Tomcat 5/6 use servlet-api.jar -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

    <target name="usage">
        <echo message=""/>
        <echo message="${name} build file"/>
        <echo message="-----------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="build     --> Build the application"/>
        <echo message="deploy    --> Deploy application as directory"/>
        <echo message="deploywar --> Deploy application as a WAR file"/>
        <echo message="install   --> Install application in Tomcat"/>
        <echo message="reload    --> Reload application in Tomcat"/>
        <echo message="start     --> Start Tomcat application"/>
        <echo message="stop      --> Stop Tomcat application"/>
        <echo message="list      --> List Tomcat applications"/>
        <echo message=""/>
    </target>

    <target name="build" description="Compile main source tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
               deprecation="false" optimize="false" failonerror="true">
            <src path="${src.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="deploy" depends="build" description="Deploy application">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
    </target>

    <target name="deploywar" depends="build" description="Deploy application as a WAR file">
        <war destfile="${name}.war"
             webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </war>
        <copy todir="${deploy.path}" preservelastmodified="true">
            <fileset dir=".">
                <include name="*.war"/>
            </fileset>
        </copy>
    </target>

     <path id="catalina-ant-classpath">
        <fileset dir="${appserver.home}/lib">
            <include name="catalina-ant.jar"/>
            <include name="tomcat-coyote.jar"/>
            <include name="tomcat-util.jar"/>
        </fileset>
        <fileset dir="${appserver.home}/bin">
            <include name="tomcat-juli.jar"/>
        </fileset>
    </path>

    <taskdef name="install" classname="org.apache.catalina.ant.DeployTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>
    <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
        <classpath refid="catalina-ant-classpath"/>
    </taskdef>

    <target name="install" description="Install application in Tomcat">
        <install url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"
                 war="${name}"/>
    </target>

    <target name="reload" description="Reload application in Tomcat">
        <reload url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="start" description="Start Tomcat application">
        <start url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="stop" description="Stop Tomcat application">
        <stop url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"
                 path="/${name}"/>
    </target>

    <target name="list" description="List Tomcat applications">
        <list url="${tomcat.manager.url}"
                 username="${tomcat.manager.username}"
                 password="${tomcat.manager.password}"/>
    </target>

</project>

这是我的 web.xml 文件:

<web-app version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

        <welcome-file-list>
            <welcome>
                index.jsp
            </welcome>
        </welcome-file-list>

</web-app>

这是我的 properties.build 文件:

appserver.home=/Users/joe/projects/tomcat
appserver.lib=${appserver.home}/lib

deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://localhost:8080/manager/text
tomcat.manager.username=someone
tomcat.manager.password=something

这是我的 tomcat-users.xml 文件:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
    <user username="someone" password="something" 
          roles="manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>

【问题讨论】:

  • 应用已部署,但启动了吗? tomcat 日志说明了什么?
  • 应用启动是什么意思?我通过终端使用 sh startup.sh 启动了 tomcat。然后我使用 ant 构建部署它。关于检查日志,我应该检查哪一个以及我应该寻找什么?有 localhost、localhost-access、catalina、catalina.out 和管理器日志。我检查了所有这些,没有错误消息,如果那是我正在寻找的。​​span>
  • 查看ant文件中的使用说明。你执行什么目标?您是否曾经执行过start 目标,正如用法所示,该目标用于启动应用程序?
  • 好吧,我还没有启动应用程序。所以现在我尝试启动 tomcat,部署应用程序,然后在终端中使用“ant start”命令启动应用程序。所有操作都表明部署和启动成功,但我仍然收到 404 错误。
  • 我解决了这个问题。我的 index.jsp 文件位于 /webapps/springapp/WEB-INF/index.jsp 中,而它本应位于 /webapps/springapp/index.jsp 中。愚蠢的错误。感谢您的帮助 JB Nizet。

标签: tomcat ant http-status-code-404


【解决方案1】:

我解决了这个问题。我的 index.jsp 文件位于 /webapps/springapp/WEB-INF/index.jsp,而它应该是 /webapps/springapp/index.jsp。愚蠢的错误。感谢您对 JB Nizet 的帮助。

【讨论】:

  • 哈哈哈,我错了,你解决了我的问题,我什至不知道为什么会这样,因为我也知道把它放在哪里是正确的!
猜你喜欢
  • 2011-03-23
  • 2019-02-27
  • 2015-05-08
  • 2018-03-13
  • 2011-03-02
  • 2017-07-07
  • 2014-10-11
  • 1970-01-01
相关资源
最近更新 更多