FileNotFoundException与JSP,但Java代码有效

时间:2019-03-26 22:18:18

标签: java eclipse tomcat servlets filenotfoundexception

我正在尝试使用jsp和java显示从csv读取到网站的信息。我正在Eclipse中使用Tomcat v9.0。

仅在Java中运行时,Java代码即可按预期工作,它将csv数据读入Song对象的ArrayList中。但是,在运行jsp文件时,它将引发filenotfoundexception。

原始的Java代码在以下位置读取并解析“ ReadBillboardCSV”的getter函数中的csv:

public ArrayList<BillboardSong> getReadBillboardCSV() throws FileNotFoundException {
    Reader in = new FileReader("testFile.csv");
    ArrayList<BillboardSong> readSongs = new ArrayList<BillboardSong>();
    try {
        Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
        for (CSVRecord record : records) {
                BillboardSong newSong = new BillboardSong();
                readSongs.add(newSong);
                newSong.setPosition(Integer.parseInt(record.get(0)));

我的servlet调用getter,然后设置属性。

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession ses = request.getSession(true);
    ReadBillboardCSV readData = new ReadBillboardCSV();
    ArrayList<BillboardSong> songsArray = readData.getReadBillboardCSV();
    ses.setAttribute("billboardArray", songsArray);
    }

这是jsp的主要部分:

<%
    RequestDispatcher rd = request.getRequestDispatcher("/ServletOne");
    rd.forward(request, response);
%>

在Tomcat上运行文件时是否有无法读取文件的原因?

0 个答案:

没有答案