0

I created simple servlet named SimpleServlet and I try to link jsp page with it:

        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);

But Tomcat 9 show me this:

HTTP Status 404 - /index.jsp

type Status report

message /index.jsp

description The requested resource is not available.

My project tree:

├── 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

My 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>
Yaz
  • 492
  • 1
  • 6
  • 20
Roman Svyatnenko
  • 699
  • 12
  • 27

1 Answers1

2

I see you are using Maven and Intellij, I also assume you did not change default maven/intellij project settings.
In the your target folder, your webapp folder is missing

  • Remove your web folder in your project root. This folder is simply not picked up by Maven/intellij when you build. The index.jsp, as a result, does not exist.
  • Create a folder called webapp under src/main.
  • Move your index.jsp under this webapp.

I also recommend that you put all your jsp view pages under webapp/WEB-INF/ so that it cannot be directly accessible, hence MVC is enforced.
This way you will access the jsp in servlet like so: /WEB-INF/index.jsp

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39