我刚刚开始学习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 我有
我看到了一些关于调度程序问题的帖子,但是当我有它时,它们在 getRequestDispatcher() 的路径之前都没有“/”。
尝试
"/WEB-INF/mvcPage.jsp"