I saw other questions like this, but none of them helped me solve my problem. Basically, I'm trying to pass a variable from a servlet to JSP.
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 did some tests with the forEach tag and configured JSTL correctly. I think the variable "data" does not reach the JSP. Any idea why?
Thanks in advance.
EDIT: To clarify porosity. I tried
<c:forEach var="i" begin="1" end="5"> Item <c:out value="${i}"/><p> </c:forEach>
And it works, but
<c:forEach var="item" items="${data}"> It worked!<p> </c:forEach>
does not work. That is what led me to think that for some reason this variable data does not reach the JSP.
EDIT 2: To start it, I configured the Tomcat server on Eclipse. I right click on the servlet and select Run As -> Run on Server. The server reboots and I start http://localhost:8080/DataExtractor/ from my browser. Here is the 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 may be the key to why this is happening. When I go to http://localhost:8080/DataExtractor/ (index.jsp), I get the html sent to Edit 2 , but if I go to http://localhost:8080/DataExtractor/DataGetterServlet , then I will get the correct page! Any idea why?