【问题标题】:Tomcat - The requested resource is not available - IntelliJ IDEA - Java EE [duplicate]Tomcat - 请求的资源不可用 - IntelliJ IDEA - Java EE [重复]
【发布时间】:2016-06-26 21:54:15
【问题描述】:

我创建了名为 SimpleServlet 的简单 servlet,并尝试将 jsp 页面与它链接:

        String varTextA = "Hello World!";
        String varTextB = "It's JSP.";

        request.setAttribute("textA", varTextA);
        request.setAttribute("textB", varTextB);

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);

但是Tomcat 9 给我看这个:

HTTP Status 404 - /index.jsp

type Status report

message /index.jsp

description The requested resource is not available.

我的项目树:

├── FirstJavaEEProject.iml
├── out
│   ├── artifacts
│   │   └── simple_mod_war_exploded
│   │       ├── index.jsp
│   │       └── WEB-INF
│   │           ├── classes
│   │           │   └── servlets
│   │           │       └── SimpleServlet.class
│   │           └── web.xml
│   └── production
│       └── simple-mod
│           └── servlets
│               └── SimpleServlet.class
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── servlets
│   │   │       └── SimpleServlet.java
│   │   └── resources
│   └── test
│       └── java
├── target
│   ├── classes
│   │   └── servlets
│   │       └── SimpleServlet.class
│   ├── first-java-ee-1.0-SNAPSHOT
│   │   ├── META-INF
│   │   │   └── MANIFEST.MF
│   │   └── WEB-INF
│   │       ├── classes
│   │       │   ├── servlets
│   │       │   │   └── SimpleServlet.class
│   │       │   └── webapp
│   │       ├── lib
...
│   │       └── web.xml
│   ├── first-java-ee-1.0-SNAPSHOT.war
│   ├── generated-sources
│   │   └── annotations
│   ├── maven-archiver
│   │   └── pom.properties
│   ├── maven-status
│   │   └── maven-compiler-plugin
│   │       ├── compile
│   │       │   └── default-compile
│   │       │       ├── createdFiles.lst
│   │       │       └── inputFiles.lst
│   │       └── testCompile
│   │           └── default-testCompile
│   │               └── inputFiles.lst
│   └── surefire
└── web
    ├── index.jsp
    └── WEB-INF
        └── web.xml

我的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">

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

    <servlet>
        <servlet-name>simple</servlet-name>
        <servlet-class>servlets.SimpleServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>simple</servlet-name>
        <url-pattern>/simple</url-pattern>
    </servlet-mapping>
</web-app>

【问题讨论】:

    标签: java jsp tomcat servlets jakarta-ee


    【解决方案1】:

    我看到您正在使用 Maven 和 Intellij,我还假设您没有更改默认的 maven/intellij 项目设置。
    在您的 target 文件夹中,您的 webapp 文件夹丢失了

    • 删除项目根目录中的 web 文件夹。构建时 Maven/intellij 根本不会拾取此文件夹。因此,index.jsp 不存在。
    • src/main 下创建一个名为 webapp 的文件夹。
    • 将您的 index.jsp 移到此 webapp 下。

    我还建议您将所有 jsp 视图页面放在 webapp/WEB-INF/ 下,这样就无法直接访问它,因此强制执行 MVC。
    这样你就可以像这样访问 servlet 中的 jsp:/WEB-INF/index.jsp

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多