最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send data from servlet to jsp, display in html table. - Stack Overflow

programmeradmin1浏览0评论

Since I don't have very much experience in web applications at all I've been struggling with this all day now.

I have a index.jsp page that sends a request to a Servlet that creates a very large csv table (5 columns, 500.000+ rows). Since the table is sorted I want to take the first 100 rows and display them in a html table. So the Servlet will redirect the request object to a table.jsp where the data is supposed to be displayed.

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:

  1. While creating the csv file, store the first 100 rows in an array of arrays and send them via request object to the jsp page, where they can be displayed in an html table.

  2. Only send the path to the csv file and read the first 100 rows directly in the jsp page.

  3. Is it useful to choose json as transfer format?

  4. Is an object containing 500 Strings too much to transfer in a request object?

  5. Is the request object a good practice at all to transfer data to the jsp page?

Thanks for your help

Since I don't have very much experience in web applications at all I've been struggling with this all day now.

I have a index.jsp page that sends a request to a Servlet that creates a very large csv table (5 columns, 500.000+ rows). Since the table is sorted I want to take the first 100 rows and display them in a html table. So the Servlet will redirect the request object to a table.jsp where the data is supposed to be displayed.

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:

  1. While creating the csv file, store the first 100 rows in an array of arrays and send them via request object to the jsp page, where they can be displayed in an html table.

  2. Only send the path to the csv file and read the first 100 rows directly in the jsp page.

  3. Is it useful to choose json as transfer format?

  4. Is an object containing 500 Strings too much to transfer in a request object?

  5. Is the request object a good practice at all to transfer data to the jsp page?

Thanks for your help

Share Improve this question edited Feb 12, 2013 at 17:09 Uooo 6,3648 gold badges38 silver badges64 bronze badges asked Feb 11, 2013 at 19:37 PogoMipsPogoMips 3,7778 gold badges30 silver badges35 bronze badges 2
  • Where is the servlet getting the data to create the csv? It sounds like this servlet is run every time the page is loaded? Seems like a data hog to me. If the data was in a database you could just make a call to the database and retrieve only the information you need. – N1tr0 Commented Feb 11, 2013 at 19:47
  • I know, this would be best of course. But I have never worked with databases before and I just don't have the time right now to get into it deep enough to realize a database implementation. for now it's okay when the servlet creates the data as it is called. – PogoMips Commented Feb 11, 2013 at 19:57
Add a ment  | 

1 Answer 1

Reset to default 2

JSP pages are piled 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 JSON as transfer format like you would do when you want to interpret your data with Javascript. JSPs are evaluated at server side.

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 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):

<%@ taglib prefix="c" uri="http://java.sun./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 MyObject is an object that holds instance variables abc, def and ghi and has getter-methods for them.

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.

发布评论

评论列表(0)

  1. 暂无评论