Java – pass variables from servlet to JSP

I have seen other problems similar to this problem, but none of them helped me solve my problem Basically, I'm trying to pass variables from servlet to JSP. 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 have completed some tests using the foreach tag and set up JSTL correctly I don't think the variable "data" reaches JSP Do you know why?

Thank you in advance

Edit: clarify poruses I tried

<c:forEach var="i" begin="1" end="5">
   Item <c:out value="${i}"/><p>
</c:forEach>

This works, but

<c:forEach var="item" items="${data}">
   It worked!<p>
</c:forEach>

It doesn't work This leads me to believe that the variable data did not reach JSP for some reason

Edit 2: in order to run it, I configured a Tomcat server on eclipse I right - click the servlet and select run as – > run on the server When the server restarts, I start http: / / localhost: 8080 / dataextractor / This is the generated 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>

Editor 3: this may be the key to this situation When I go to http: / / localhost: 8080 / dataextractor / (index. JSP), I get the HTML published in edit 2, but if I go to http: / / localhost: 8080 / dataextractor / datagetterservlet, I do get the right page! Do you know why?

Solution

This may be a typo, and $(item) should be the following ${item} –

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

to update

Http: / / localhost: 8080 / dataextractor / does not map to servlets, while http: / / localhost: 8080 / dataextractor / datagetterservlet maps to servlets If the servlet is not invoked, then obviously the data will not be a request In other words, the first URL does not call the servlet, but directly talks to you (you may have a welcome page in web. XML)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>