这是我的Servlet
班级
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
@WebServlet("/Register")
public class Register extends HttpServlet {
static int id=0;
private static final long serialVersionUID = 1L;
private static final ArrayList<Employee> EMPLOYEES = new ArrayList<>();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
id++;
int uage=0;
long mob=0;
String uexit = request.getParameter("uexit");
if(uexit.equals("Exit")) {
System.out.println("\n\n\n\n\n\n\n\n");
request.getRequestDispatcher("exit.jsp").forward(request, response);
}
else {
String uname = request.getParameter("uname");
uage = Integer.parseInt(request.getParameter("uage"));
mob = Long.parseLong(request.getParameter("umob"));
if(uname!=null && uage!=0 && mob!=0) {
Employee employee = new Employee(id, uname, uage, mob);
EMPLOYEES.add(employee);
request.getSession().setAttribute("employees" , EMPLOYEES);
RequestDispatcher rd = getServletContext().getRequestDispatcher("register.jsp");
rd.forward(request, response);
}
else {
request.getSession().setAttribute("employees", null);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
for(int i=0; i<EMPLOYEES.size(); i++) {
System.out.print(EMPLOYEES.get(i).id + "\t");
System.out.print(EMPLOYEES.get(i).name + "\t");
System.out.print(EMPLOYEES.get(i).age + "\t");
System.out.print(EMPLOYEES.get(i).mob + "\t");
System.out.println();
}
}
这是我的register.jsp
页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MyProject</title>
</head>
<h2 align="center">
<font color="#BF360C" size="10" face="Times New Roman"><b><i>
Register</i></b></font>
</h2>
<body style="background-color: #AED6F1">
<form action="Register" method="POST">
<table align="center" cellpadding="20" width="600"
style="background-color: #33C3DD;">
<tr>
<th><font size="5" face="Times New Roman"><b><i>Name:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uname"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Age:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uage"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Mobile
Number:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="umob"></td>
</tr>
<tr>
<th colspan="3" align="center"><input type="submit"
name="uexit" value="Register"><br /> <br /> <input
type="submit" name="uexit" value="Exit"></th>
</tr>
</table>
</form>
<br>
<br>
<table cellpadding="20" cellspacing="5" width="900"
style="background-color: #FDFEFE;" align="center">
<tr style="background-color: #FDFDFD">
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Sr. No.</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Name</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Age</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Mobile Number:</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Edit</i></b></font></th>
</tr>
<c:forEach items="${employees}" var="item">
<tr>
<td style="background-color: #4468D1"><c:out value = "${item.id}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.name}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.age}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.mob}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
这是我的Entity
班级
public class Employee {
public int id;
public String name;
public int age;
public long mob;
public Employee() {}
public Employee(int id, String name, int age, long mob) {
this.name= name;
this.age= age;
this.mob= mob;
this.id=id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=age;
}
public long getMob() {
return mob;
}
public void setMob(int mob) {
this.mob = mob;
}
}
我想将servlet中的列表EMPLOYEES传递给jsp页面abd想要用
Employee
显示每个<c:forEach>
对象,但它一直在说异常。所以请任何人回答我。
如果我使用tomcat 8.5,还告诉我要下载哪个jstl标准jar 我有jstl-jar 1.2,但我不认为它有效,因为无论我怎么做,这个错误都会继续存在。
答案 0 :(得分:1)
尝试使用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
而不是
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
在你的jsp代码中
答案 1 :(得分:1)
似乎你在这里做错了什么
<tr>
<td>${item}</td>
</tr>
根据您的代码,您将获得item
中的对象列表。如何打印包含对象的列表?
尝试使用
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.age}</td>
<td>${item.mob}</td>
</tr>