Javascript 将数据从servlet发送到jsp,显示在html表中。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14819537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send data from servlet to jsp, display in html table.
提问by PogoMips
Since I don't have very much experience in web applications at all I've been struggling with this all day now.
因为我在 Web 应用程序方面没有太多经验,所以我一整天都在为此苦苦挣扎。
I have a index.jsppage that sends a request to a Servletthat creates a very large csvtable (5 columns, 500.000+ rows). Since the table is sorted I want to take the first 100 rows and display them in a htmltable. So the Servletwill redirect the request object to a table.jspwhere the data is supposed to be displayed.
我有一个index.jsp页面向Servlet创建一个非常大的csv表(5 列,500.000+ 行)的请求发送请求。由于表格已排序,我想取前 100 行并将它们显示在html表格中。因此,Servlet将请求对象重定向table.jsp到应该显示数据的位置。
I guess I need a piece of advice on the general approach on this. There a several approaches and questions that came to my mind:
我想我需要关于这方面的一般方法的建议。我想到了几种方法和问题:
While creating the
csvfile, store the first 100 rows in an array of arrays and send them via request object to thejsppage, where they can be displayed in anhtml table.Only send the path to the
csvfile and read the first 100 rows directly in thejsppage.Is it useful to choose
jsonas transfer format?Is an object containing 500 Strings too much to transfer in a request object?
Is the request object a good practice at all to transfer data to the
jsppage?
创建
csv文件时,将前 100 行存储在一个数组数组中,并通过请求对象将它们发送到jsp页面,在那里它们可以显示在html table.只发送
csv文件的路径并直接在jsp页面中读取前 100 行。选择
json作为传输格式有用吗?包含 500 个字符串的对象是否太多而无法在请求对象中传输?
请求对象是否是将数据传输到
jsp页面的好习惯?
Thanks for your help
谢谢你的帮助
回答by Uooo
JSP pages are compiled into servlets by the container, so they are servlets in the end and you can use Java Code in them. So there is no need to use JSONas transfer format like you would do when you want to interpret your data with Javascript. JSPs are evaluated at server side.
JSP页面被容器编译成servlet,所以归根结底就是servlet,可以在其中使用Java Code。因此,JSON当您想使用 Javascript 解释数据时,无需像使用传输格式那样使用传输格式。JSP 在服务器端进行评估。
So, I would do it like this:
所以,我会这样做:
- In your servlet, retreive the data
- Forward the request to the JSP (this happens on server-side; the client (browser) does not recognise this step like in a redirection)
- Build the table in the JSP (and render the response)
- 在您的 servlet 中,检索数据
- 将请求转发到 JSP(这发生在服务器端;客户端(浏览器)不像重定向那样识别这一步)
- 在 JSP 中构建表(并呈现响应)
Servlet code:
服务端代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<MyObject> listData = ...; // however you get the data
// set the attribute in the request to access it on the JSP
request.setAttribute("listData", listData);
RequestDispatcher rd = getServletContext()
.getRequestDispatcher("/path/to/page.jsp");
rd.forward(request, response);
}
JSP (using JSTL taglib):
JSP(使用 JSTL 标签库):
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- html, head and starting body tag ... -->
<table>
<c:forEach var="element" items="${listData}">
<tr>
<td>${element.abc}</td>
<td>${element.def}</td>
<td>${element.ghi}</td>
</tr>
</c:forEach>
</table>
Where MyObjectis an object that holds instance variables abc, defand ghiand has getter-methods for them.
哪里MyObject是持有实例变量的对象abc,def并ghi与具有消气方法他们。
Note that you need the JSTL jar (which can be downloaded here) in your WEB-INF/lib folder if you do not have it already.
请注意,如果您还没有 WEB-INF/lib 文件夹中的 JSTL jar(可以在此处下载),则需要它。

