Passing variable from servlet to JSP

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?

+6
source share
3 answers

It could be a typo, $(item) should be ${item} in the following -

 <c:forEach var="column" items="$(item)" > 

Update

http://localhost:8080/DataExtractor/ , which does not map to the servlet, but http://localhost:8080/DataExtractor/DataGetterServlet . If the servlet is not called, then it is obvious that data will not be a request. In other words, the first url does not invoke the servlet, but directly tells you about the page. (You probably have a welcome page in web.xml)

+4
source

In jsp you need to include this in the header:

 <jsp:useBean id="data" class="java.util.ArrayList" scope="request"/> 
+2
source

I think your problem was due to only typos.

 <c:forEach var = "column" items = "${data}"> 

and

 <c:forEach var = "column" items = "${requestScope.data}"> 

worked well for me because requestScope stores a map of request objects.

+2
source

Source: https://habr.com/ru/post/947251/


All Articles