简体   繁体   中英

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

The first part, reading and displaying the CSV file, is easy. 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. 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. Any form of guidance would be appreciated.

You should use opencsv or java csv for this. Break your problem into little requirements.

  1. Use those libraries to extract data.
  2. Learn how to display the data on the jsp
  3. Learn how to download a file from a jsp link
  4. Learn how to write a csv with those libraries
  5. Combine those steps so you can download the csv you write from the jsp

You are right that a servlet is a better tool for most of this. The servlet should be responsible for extracting the data and writing the file. It then forwards this data off to the jsp. The jsp should only have display logic in it. It should have as little processing logic as possible. Keep the processing logic in the servlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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