简体   繁体   English

将变量从servlet传递到JSP

[英]Passing variable from servlet to JSP

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. 基本上,我正在尝试将变量从servlet传递给JSP。

The servlet code. servlet代码。

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 我的JSP代码

<%@ 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. 我已经使用forEach标签完成了一些测试,并正确设置了JSTL。 I think the variable "data" is not reaching the JSP. 我认为变量“data”没有到达JSP。 Any idea why? 知道为什么吗?

Thanks in advance. 提前致谢。

EDIT: For clarification porpuses. 编辑:澄清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. 这使我相信可变数据由于某种原因没有到达JSP。

EDIT 2: To run it, I configured a Tomcat server on Eclipse. 编辑2:为了运行它,我在Eclipse上配置了一个Tomcat服务器。 I right click on the servlet and choose Run As -> Run on Server. 我右键单击servlet并选择Run As - > Run on Server。 The server restarts and I launch http://localhost:8080/DataExtractor/ from my browser. 服务器重新启动,我从浏览器启动http://localhost:8080/DataExtractor/ Here's the resulting html: 这是生成的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. 编辑3:这可能是发生这种情况的关键。 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! 当我转到http://localhost:8080/DataExtractor/ )时,我得到了在Edit 2中发布的html,但如果我转到http://localhost:8080/DataExtractor/DataGetterServlet那么我确实得到了右页! Any idea why? 知道为什么吗?

This might be a typo, $(item) should be ${item} in the following - 这可能是一个错字, $(item)应该是以下的${item} -

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

Update 更新

http://localhost:8080/DataExtractor/ that doesn't map to the servlet, while http://localhost:8080/DataExtractor/DataGetterServlet does. http://localhost:8080/DataExtractor/不映射到servlet,而http://localhost:8080/DataExtractor/DataGetterServlet则映射到servlet。 If the servlet isn't invoked then it's obvious that data is not going to be the request. 如果没有调用servlet,那么很明显data不会是请求。 In other words, the first url is not invoking the servlet, but directly talking you to the page. 换句话说,第一个url不是调用servlet,而是直接与你交谈。 (You probably have as welcome-page in web.xml) (你可能在web.xml中有欢迎页面)

在jsp中,您需要在标头中包含它:

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

I think your problem was because of typo only. 我认为你的问题只是因为拼写错误。

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

and

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

worked well for me because requestScope holds the map of request objects. 我工作得很好,因为requestScope保存了请求对象的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM