这是我的程序,用于从 servlet 获取包含 bean-object 的 ArrayList 并使用 jstl tag 在 jsp 中显示其内容。我运行程序后没有显示结果。请查看我的代码并帮助我找到错误。我是jstl的新手。
小服务程序代码
package com.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;
import com.servlet.mybean;
@WebServlet("/Bean")
public class Bean extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Bean() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
mybean mybean_obj = new mybean();
ArrayList<mybean> obj=new ArrayList<mybean>();
mybean_obj.id=100;
mybean_obj.name="vishal";
mybean_obj.roll=1225;
mybean mybean_obj1 = new mybean();
mybean_obj1.id=101;
mybean_obj1.name="anand";
mybean_obj1.roll=1226;
mybean mybean_obj2 = new mybean();
mybean_obj2.id=102;
mybean_obj2.name="google";
mybean_obj2.roll=1557;
mybean mybean_obj3 = new mybean();
mybean_obj3.id=103;
mybean_obj3.name="yahoo";
mybean_obj3.roll=1558;
obj.add(mybean_obj);
obj.add(mybean_obj1);
obj.add(mybean_obj2);
obj.add(mybean_obj3);
HttpSession session = request.getSession();
session.setAttribute("mybean", obj);
request.setAttribute("mybean", obj);
RequestDispatcher fwd = request.getRequestDispatcher("encodeUrl.jsp");
fwd.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Encode URL </title>
</head>
<body>
<c:forEach var="start" items="${sessionScope.mybean}">
${sessionScope.start.id}<br/>${sessionScope.start.roll }<br/>${sessiontScope.start.name }<hr/>
</c:forEach>
</body>
</html>
我从主页调用 servlet,从 servlet 请求被转发到上面代码的最终 jsp 页面。