我哪里配置错了造成tomcat输出404?

发布于 2022-09-04 09:23:55 字数 11555 浏览 35 评论 0

我依据《架构探险从零开始写javaweb框架》里的案列做。
IntelliJ-IDEA 2016.3.2
Tomcat7

这个是404截图
图片描述

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.donman</groupId>
    <artifactId>chapter1</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF08</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--Servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--JSP-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!--JSTL-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jst1</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/${project.artifactId}</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <url>http://repo2.maven.org/maven2</url>
        </repository>
    </repositories>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
</web-app>

hello.jsp

<%@ page pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Hello</h1>
<h2>当前时间:${currentTime}</h2>
</body>
</html>

HelloServlet.java

package com.donman.chapter1;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lindi on 2017/1/2.
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateformat.format(new Date());
        req.setAttribute("currentTime", currentTime );
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
    }

}

log:
C:UserslindiIdeaProjectsapache-tomcat-7.0.73bincatalina.bat run
[2017-01-03 12:59:51,566] Artifact chapter1:war exploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE: "C:Userslindi.IntelliJIdea2016.3systemtomcatUnnamed_(1)_chapter1"
Using CATALINA_HOME: "C:UserslindiIdeaProjectsapache-tomcat-7.0.73"
Using CATALINA_TMPDIR: "C:UserslindiIdeaProjectsapache-tomcat-7.0.73temp"
Using JRE_HOME: "C:Program FilesAndroidAndroid Studio1jre"
Using CLASSPATH: "C:UserslindiIdeaProjectsapache-tomcat-7.0.73binbootstrap.jar;C:UserslindiIdeaProjectsapache-tomcat-7.0.73bintomcat-juli.jar"
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version: Apache Tomcat/7.0.73
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built: Nov 7 2016 21:27:23 UTC
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number: 7.0.73.0
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name: Windows 10
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version: 10.0
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture: amd64
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home: C:Program FilesAndroidAndroid Studio1jrejre
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version: 1.8.0_76-release-b03
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor: Oracle Corporation
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE: C:Userslindi.IntelliJIdea2016.3systemtomcatUnnamed_(1)_chapter1
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME: C:UserslindiIdeaProjectsapache-tomcat-7.0.73
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcom.sun.management.jmxremote=
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcom.sun.management.jmxremote.port=1099
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcom.sun.management.jmxremote.ssl=false
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.rmi.server.hostname=127.0.0.1
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.util.logging.config.file=C:Userslindi.IntelliJIdea2016.3systemtomcatUnnamed_(1)_chapter1conflogging.properties
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=C:UserslindiIdeaProjectsapache-tomcat-7.0.73endorsed
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=C:Userslindi.IntelliJIdea2016.3systemtomcatUnnamed_(1)_chapter1
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=C:UserslindiIdeaProjectsapache-tomcat-7.0.73
一月 03, 2017 12:59:53 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.io.tmpdir=C:UserslindiIdeaProjectsapache-tomcat-7.0.73temp
一月 03, 2017 12:59:53 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: Loaded APR based Apache Tomcat Native library 1.2.10 using APR version 1.5.2.
一月 03, 2017 12:59:53 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
一月 03, 2017 12:59:55 下午 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized (OpenSSL 1.0.2j 26 Sep 2016)
一月 03, 2017 12:59:55 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-apr-8080"]
一月 03, 2017 12:59:55 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-apr-8009"]
一月 03, 2017 12:59:55 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 2681 ms
一月 03, 2017 12:59:55 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
一月 03, 2017 12:59:55 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.73
一月 03, 2017 12:59:55 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
一月 03, 2017 12:59:55 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
一月 03, 2017 12:59:55 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 266 ms
Connected to server
[2017-01-03 12:59:55,741] Artifact chapter1:war exploded: Artifact is being deployed, please wait...
[2017-01-03 12:59:56,949] Artifact chapter1:war exploded: Artifact is deployed successfully
[2017-01-03 12:59:56,949] Artifact chapter1:war exploded: Deploy took 1,208 milliseconds
一月 03, 2017 1:00:05 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:UserslindiIdeaProjectsapache-tomcat-7.0.73webappsmanager
一月 03, 2017 1:00:06 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:UserslindiIdeaProjectsapache-tomcat-7.0.73webappsmanager has finished in 521 ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇见了你 2022-09-11 09:23:55

之前没注意看,现在仔细看了一下你的问题。
HelloServlet.java 中 的forward 地址不能转发到WEB-INF 目录下的hello.jsp,因为这个目录是不能被客户端访问的。
解决办法是把.jsp文件移出到WEN-INF父目录下,也就是ROOT

你在我安 2022-09-11 09:23:55

req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);

π浅易 2022-09-11 09:23:55

撸主写东西很不细心

    <!--JSTL-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jst1</artifactId>

这个地方是jstl,不是jst1

我也遇到你这个问题,但一直没搞定。有个解决办法,jdk换1.8,TOMCAT换8,pom.xml里改一下JDK版本1.6-->1.8就行,别的不用动。jsp/hello.jsp要放在 WEB-INF 目录下。
这样就没问题了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文