404 当我尝试使用 Servlet [重复]

问题描述 投票:0回答:2

我刚刚开始学习Java Servlet。我按照我开始的课程做任务,似乎一切都是一样的,但由于某种原因,它对我不起作用。

我正在尝试将数据从 servlet 传递到 jsp 页面。

我有以下代码:

MvcServlet.java

package com.company.servletdemo;

import javax.servlet.RequestDispatcher;
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;

@WebServlet(name = "MvcServlet", urlPatterns = "/MvcServlet")

public class MvcServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] students = {"John", "Alex", "Michael"};
        request.setAttribute("student_list", students);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/mvcPage.jsp");

        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

还有

mvcPage.jsp

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<body>

<c:forEach var="tempStudent" items="${student_list}">

    ${tempStudent} <br/>

</c:forEach>

</body>
</html>

当我去

http://localhost:8080/servletdemo/MvcServlet
时,我有404 error

这是我的project structure

我看到了一些关于调度程序问题的帖子,但是当我有它时,它们在

getRequestDispatcher()
中的路径之前都没有“/”。

servlets http-status-code-404
2个回答
0
投票

尝试

"/WEB-INF/mvcPage.jsp"

0
投票

您的应用程序有问题。这不是雅加达。您应该更新您的库或使用 Tomcat 9.x 进行部署。

如需进一步阅读,请参阅this答案。

© www.soinside.com 2019 - 2024. All rights reserved.