gpt4 book ai didi

java - 为什么我的 servlet 不转发到我的 .jsp?

转载 作者:行者123 更新时间:2023-12-02 03:00:57 25 4
gpt4 key购买 nike

这是我的代码:

Index.html

<html>
<head>
<title>Web App Assignment 5</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<a href="listEmployees.jsp">View Employees</a>
</div>
</body>
</html>

Employees.java

class Employees {

private String name;
private int id;

public Employees() {

}
public Employees(String name, int id){
setName(name);
setID(id);
}

public String getName(){
return name;
}

public int getID(){
return id;
}

public void setName(String name){
this.name = name;
}

public void setID(int id) {
this.id = id;
}
}

getEmployees.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class getEmployees extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

Employees emp1 = new Employees("Jim", 002);
Employees emp2 = new Employees("Pam", 043);
Employees emp3 = new Employees("Dwight", 013);
Employees emp4 = new Employees("Kevin", 123);
Employees emp5 = new Employees("Michael", 001);
Employees emp6 = new Employees("Oscar", 033);

List<Employees> employees = new ArrayList<Employees>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
employees.add(emp4);
employees.add(emp5);
employees.add(emp6);

HttpSession session = request.getSession();
session.setAttribute("empl", employees);

RequestDispatcher dispatcher = request.getRequestDispatcher("listEmployees.jsp");
dispatcher.forward(request, response);
}
}
}

listEmployees.jsp

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List of Employees </title>
</head>
<body>
<h1><u> List of Employees </u> </h1>
<table>
<thead>
<tr>

<td>Employee Name</td>
<td></td>
<td>Employee ID</td>
</tr>
</thead>
<c:forEach items="${empl}" var="current">
<tr>
<td><c:out value="${empl}" /><td>
</tr>
</c:forEach>
</table>
</body>
</html>

我试图将索引页转发到 getEmployees servlet,然后该 servlet 会将员工列表转发到 JSP 并显示员工列表。我不确定为什么 servlet 不转发到 jsp。任何帮助将非常感激。使用 Glassfish 服务器。

最佳答案

您必须更改在 foreach 中打印的变量:

<c:forEach items="${empl}" var="current">
<tr>
<td><c:out value="${current.name}" /><td>
<td><td>
<td><c:out value="${current.id}" /><td>
</tr>
</c:forEach>

关于java - 为什么我的 servlet 不转发到我的 .jsp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42370498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com