Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variable from servlet to JSP
    primarykey
    data
    text
    <p>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.</p> <p>The servlet code.</p> <pre><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&lt;String[]&gt; 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); } } </code></pre> <p>My JSP code</p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Data extractor&lt;/title&gt; &lt;/head&gt; &lt;body&gt; Data table: &lt;table boder="1"&gt; &lt;c:forEach var="item" items="${data}" &gt; &lt;tr&gt; &lt;c:forEach var="column" items="${item}"&gt; &lt;td&gt;${column}&lt;/td&gt; &lt;/c:forEach&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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?</p> <p>Thanks in advance.</p> <p><strong>EDIT:</strong> For clarification porpuses. I have tried </p> <pre><code>&lt;c:forEach var="i" begin="1" end="5"&gt; Item &lt;c:out value="${i}"/&gt;&lt;p&gt; &lt;/c:forEach&gt; </code></pre> <p>And that works, but</p> <pre><code>&lt;c:forEach var="item" items="${data}"&gt; It worked!&lt;p&gt; &lt;/c:forEach&gt; </code></pre> <p>doesn't work. This is what has led me to believe that that variable data is not reaching the JSP for some reason.</p> <p><strong>EDIT 2:</strong> 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 <code>http://localhost:8080/DataExtractor/</code> from my browser. Here's the resulting html:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Data extractor&lt;/title&gt; &lt;/head&gt; &lt;body&gt; Data table: &lt;table border="1"&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>EDIT 3:</strong> This might be the key of why this is happening. When I go to <code>http://localhost:8080/DataExtractor/</code> (index.jsp), I get the html posted in <strong>Edit 2</strong>, but if I go to <code>http://localhost:8080/DataExtractor/DataGetterServlet</code> then I do get the right page! Any idea why?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload