I've seen other questions similar to this asked but none of them have helped me solve my problem. Basically, I'm trying to pass a variable from a servlet to a JSP.
The servlet code.
package com.servlets;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.DataGetter;
@WebServlet("/DataGetterServlet")
public class DataGetterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
ArrayList<String[]> data;
private DataGetter dg;
public void init() throws ServletException {
try {
dg = new DataGetter();
data = dg.getData();
} catch (Exception e) {
throw new ServletException("An exception occurred in DataGetterServlet: "
+ e.getClass().getName());
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("data", data);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
My JSP code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Data extractor</title>
</head>
<body>
Data table:
<table boder="1">
<c:forEach var="item" items="${data}" >
<tr>
<c:forEach var="column" items="${item}">
<td>${column}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
I have done some tests with the forEach tag and JSTL is setup properly. I think the variable "data" is not reaching the JSP. Any idea why?
Thanks in advance.
EDIT: For clarification porpuses. I have tried
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
And that works, but
<c:forEach var="item" items="${data}">
It worked!<p>
</c:forEach>
doesn't work. This is what has led me to believe that that variable data is not reaching the JSP for some reason.
EDIT 2: To run it, I configured a Tomcat server on Eclipse. I right click on the servlet and choose Run As -> Run on Server. The server restarts and I launch http://localhost:8080/DataExtractor/
from my browser. Here's the resulting html:
<!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>Data extractor</title>
</head>
<body>
Data table:
<table border="1">
</table>
</body>
</html>
EDIT 3: This might be the key of why this is happening. When I go to http://localhost:8080/DataExtractor/
(index.jsp), I get the html posted in Edit 2, but if I go to http://localhost:8080/DataExtractor/DataGetterServlet
then I do get the right page! Any idea why?
data.get(0)[0]
to check and the ArrayList and internal arrays are populated correctly. – Socle