简体   繁体   English

使用JSP写入CSV文件

[英]Writing to a CSV file using JSP

I need to have a JSP file that reads a CSV, displays its contents, and has the ability to delete and add rows. 我需要一个JSP文件,该文件可以读取CSV,显示其内容并具有删除和添加行的功能。 I have java coding experience but next to no experience with JSP (I've been frantically researching since my job requires this JSP to be produced today though). 我有Java编码经验,但几乎没有JSP经验(因为我的工作要求今天就生产此JSP,所以我一直在进行疯狂的研究)。

The first part, reading and displaying the CSV file, is easy. 第一部分,读取和显示CSV文件很容易。 However, I really have no idea how to export the data to a file since javascripts apparently don't have that functionality and I wanted to make an add row button that takes the data from text fields and adds an extra row to the CSV file. 但是,我真的不知道如何将数据导出到文件中,因为javascript显然不具备该功能,我想创建一个添加行按钮,以从文本字段中获取数据并向CSV文件添加额外的一行。 I have read that a Servlet would be a better tool to use but I do not have enough experience to know how to incorporate a Servlet into a jsp file. 我已经读过Servlet会是更好的工具,但是我没有足够的经验来知道如何将Servlet合并到jsp文件中。 Any form of guidance would be appreciated. 任何形式的指导将不胜感激。

You should use opencsv or java csv for this. 您应该为此使用opencsvjava csv Break your problem into little requirements. 将您的问题分解成很少的要求。

  1. Use those libraries to extract data. 使用这些库提取数据。
  2. Learn how to display the data on the jsp 了解如何在jsp上显示数据
  3. Learn how to download a file from a jsp link 了解如何从jsp链接下载文件
  4. Learn how to write a csv with those libraries 了解如何使用这些库编写csv
  5. Combine those steps so you can download the csv you write from the jsp 结合这些步骤,以便您可以下载从jsp编写的csv

You are right that a servlet is a better tool for most of this. 您是正确的,对于大多数情况而言,servlet是更好的工具。 The servlet should be responsible for extracting the data and writing the file. Servlet应该负责提取数据并写入文件。 It then forwards this data off to the jsp. 然后,它将这些数据转发给jsp。 The jsp should only have display logic in it. jsp应该只包含显示逻辑。 It should have as little processing logic as possible. 它应具有尽可能少的处理逻辑。 Keep the processing logic in the servlet. 将处理逻辑保留在servlet中。

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

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