HTTP Status 404 – 未找到

HTTP Status 404 – 未找到

# 具体遇到的问题
1.我右键show.jsp,第一次在二级菜单上有"run show.jsp"对象,第二次显示的是"run show.do"?

2.浏览器http那一栏是http://localhost:8080/show.do,手动输入http://localhost:8080/sm/show.do还是报错为404.

3.控制台显示为:

      

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器构建:            Mar 11 2020 10:01:39 UTC

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服务器版本号(:8.5.53.0

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 7

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS.版本:               6.1

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 架构:                  amd64

12-Dec-2020 23:57:25.814 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java 环境变量:         E:\Program Files\Java\jdk1.8.0_121\jre

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM 版本:              1.8.0_121-b13

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM.供应商:            Oracle Corporation

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\Administrator\.IntelliJIdea2018.1\system\tomcat\show_jsp_(2)_sm

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\apache-tomcat-8.5.53

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\Administrator\.IntelliJIdea2018.1\system\tomcat\show_jsp_(2)_sm\conf\logging.properties

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=

12-Dec-2020 23:57:25.815 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\Administrator\.IntelliJIdea2018.1\system\tomcat\show_jsp_(2)_sm

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\apache-tomcat-8.5.53

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\apache-tomcat-8.5.53\temp

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].

12-Dec-2020 23:57:25.816 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].

12-Dec-2020 23:57:25.818 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL配置:useAprConnector[false],useOpenSSL[true]

12-Dec-2020 23:57:25.823 信息 [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]

12-Dec-2020 23:57:25.939 信息 [main] org.apache.coyote.AbstractProtocol.init 初始化协议处理器 ["http-nio-8080"]

12-Dec-2020 23:57:25.954 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read

12-Dec-2020 23:57:25.965 信息 [main] org.apache.catalina.startup.Catalina.load Initialization processed in 430 ms

12-Dec-2020 23:57:25.992 信息 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]

12-Dec-2020 23:57:25.992 信息 [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.53

12-Dec-2020 23:57:26.004 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]

12-Dec-2020 23:57:26.014 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 48 ms

Connected to server

[2020-12-12 11:57:26,424] Artifact sm_web:war: Artifact is being deployed, please wait...

12-Dec-2020 23:57:27.569 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。

[2020-12-12 11:57:27,626] Artifact sm_web:war: Artifact is deployed successfully

[2020-12-12 11:57:27,627] Artifact sm_web:war: Deploy took 1,202 milliseconds

12-Dec-2020 23:57:36.006 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [D:\apache-tomcat-8.5.53\webapps\manager]

12-Dec-2020 23:57:36.036 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\apache-tomcat-8.5.53\webapps\manager] has finished in [30] ms


# 报错信息的截图
http://img1.sycdn.imooc.com//climg/5fd4ead0097c591e15150454.jpg

# 相关课程内容截图
http://img1.sycdn.imooc.com//climg/5fd4ebc20925911615130872.jpg

# 尝试过的解决思路和结果
我的IDEA的文件目录结构如下:

http://img1.sycdn.imooc.com//climg/5fd4ec6009f9ef6719201019.jpg# 粘贴全部相关代码,切记添加代码注释(请勿截图)

//DispatcherServlet.java

package com.imooc.sm.global;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DispatcherServlet extends GenericServlet {

private ApplicationContext context;

public void init() throws ServletException {
super.init();
context = new ClassPathXmlApplicationContext("spring.xml");
}

@Override
   public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;

/**
        *          /staff/add.do      /login.do
        *          staffController
        *          public void add(ServletRequest servletRequest, ServletResponse servletResponse)
        */

       String path = request.getServletPath().substring(1);
String beanName = null;
String methodName = null;
int index = path.indexOf('/');
if(index != -1){
beanName = path.substring(0, index)+"Controller";
methodName = path.substring(index+1, path.indexOf(".do"));
}else{
beanName = "selfController";
methodName = path.substring(0, path.indexOf(".do"));
}

Object obj = context.getBean(beanName);
try {
Method method = obj.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(obj, request, response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}


//show.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/12/12
Time: 23:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
你好,世界!
</body>
</html>


​//web.xml
​<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<filter>
<filter-name>Encoding</filter-name>
<filter-class>com.imooc.sm.global.EncodingFilter</filter-class>
<init-param>
<param-name>ENCODING</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>Global</servlet-name>
<servlet-class>com.imooc.sm.global.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Global</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>


正在回答

登陆购买课程后可参与讨论,去登陆

4回答

同学你好,1、同学打开target目录下的sm_web没有show.jsp吧

http://img1.sycdn.imooc.com//climg/5fd96bc709a0176f04910614.jpg

如果是,可能是编译问题,同学可以尝试手动将show.jsp复制粘贴到编译后的sm_web目录下。或者同学重新创建一个项目再来试试。

  • 慕粉1465475474 提问者 #1

    复制了就运行没有404了,应该是编译的问题,谢谢老师。

    2020-12-16 21:48:36
好帮手慕小班 2020-12-15 10:36:12

同学你好,1、检查同学贴出的配置信息,并没有问题。

    ​2、404是没有在对应位置找到资源,同学可以检查一下自己target编译后的目录是不是有show.jsp页面,比如:

http://img1.sycdn.imooc.com//climg/5fd820a4096d4cc304000846.jpg

    ​3、同学可以尝试清理一下服务器和浏览器缓存,重新启动项目,再来尝试一下

清理缓存

http://img1.sycdn.imooc.com//climg/5fd820f50958bfb302810418.jpg

重构项目

http://img1.sycdn.imooc.com//climg/5fd820d6093e0b7902620157.jpg

清理浏览器缓存

http://img1.sycdn.imooc.com//climg/5fd8210d09883fd005460301.jpg

如上清理后再来试试。

  • 提问者 慕粉1465475474 #1

    已经按老师的方法清除缓存了,target编译后的目录还是没有show.jsp,我要怎样才能有show.jsp呢,老师请看截图:



    http://img1.sycdn.imooc.com//climg/5fd8bb7b09ac254505340691.jpg

    http://img1.sycdn.imooc.com//climg/5fd8bb7c095ce32705370690.jpg


    2020-12-15 21:36:28
好帮手慕小班 2020-12-14 09:50:57

同学你好,1、同学可以将自己的配置内容贴出,比如:

http://img1.sycdn.imooc.com//climg/5fd6c44209a61b8607890528.jpg

http://img1.sycdn.imooc.com//climg/5fd6c45609113a8f08850234.jpg

如上所示的配置内容贴出。

2、同学的目录结构有没有修改,如果有变化,建议同学将自己修改后的目录结构贴出。

3、同学可以将自己修改为8080的浏览器访问报错截图贴出(将地址栏也加上)。


  • 提问者 慕粉1465475474 #1

    按老师要求,截图如下:

    http://img1.sycdn.imooc.com//climg/5fd7642b090714ca10920721.jpg

    http://img1.sycdn.imooc.com//climg/5fd7642b09d23f9c10920721.jpg

    http://img1.sycdn.imooc.com//climg/5fd764b009527eb905030921.jpg

    http://img1.sycdn.imooc.com//climg/5fd765140983168f15120349.jpg

    http://localhost:8080/show.jsp


    2020-12-14 21:14:35
好帮手慕小班 2020-12-13 10:50:36

同学你好,404是访问路径找不到,也就是在对应路径下并没有正确加载到对应资源(文件)。

猜测是同学地址书写有误,因为当前show.jsp并没有配置.do的访问内容,地址应该是直接访问这个jsp页面,而不是.do的内容

另外问一下同学是否配置了项目名,例如:

http://img1.sycdn.imooc.com//climg/5fd5815f09bbef0b08030552.jpg

http://img1.sycdn.imooc.com//climg/5fd58170097b4f4c09090260.jpg

访问结果如下:

http://img1.sycdn.imooc.com//climg/5fd57f320975df3304490150.jpg


  • 提问者 慕粉1465475474 #1

    老师,我把《第3章 案例准备及控制模块开发》视频对着自己的代码又看了一遍,没有发现什么问题。后来百度了一下《IDEA怎么启动Tomcat》还是没有解决,现在关键问题是配置不对,我在浏览器输入localhost:8080,结果还是显示404,老师,帮帮忙?

    2020-12-13 21:49:12
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师