csvファイルを読み込んでみよう。今回はそこで作成したオブジェクトをアプリケーションスコープに配置する。
実行すると、csvファイルの内容をテーブルで出力される。
[実行例]

[作例]
1.CSVLessonプロジェクトを作成する。
2.以下からsample.csvをダウンロードする。
3.WebContentフォルダ内にdataフォルダを作成し、その中にsample.csvをコピペによって配置する。

Model
4.modelパッケージを作成しその中にSales.javaを作成する。
●model.Sales.java
03 | import java.io.Serializable; |
05 | public class Sales implements Serializable{ |
07 | private String salesA; |
08 | private String salesB; |
10 | public Sales(String year,String salesA,String salesB){ |
16 | public String getYear() { |
19 | public void setYear(String year) { |
22 | public String getSalesA() { |
25 | public void setSalesA(String salesA) { |
28 | public String getSalesB() { |
31 | public void setSalesB(String salesB) { |
5.modelパッケージ内にSalesParser.javaを以下のように作成する。
03 | import java.io.BufferedReader; |
04 | import java.io.FileInputStream; |
05 | import java.io.FileNotFoundException; |
06 | import java.io.IOException; |
07 | import java.io.InputStreamReader; |
08 | import java.io.UnsupportedEncodingException; |
09 | import java.util.ArrayList; |
12 | public class SalesParser { |
13 | public List<Sales> getList(String path){ |
14 | List<Sales> list= new ArrayList<>(); |
15 | BufferedReader br= null ; |
17 | FileInputStream fis= new FileInputStream(path); |
18 | InputStreamReader isr= new InputStreamReader(fis, "UTF-8" ); |
19 | br= new BufferedReader(isr); |
21 | while ((line=br.readLine()) != null ){ |
22 | if (line.startsWith( "年度" )){ |
32 | String[] data=line.split( "," ,- 1 ); |
34 | String salesA=data[ 1 ]; |
35 | String salesB=data[ 2 ]; |
36 | Sales sales= new Sales(year,salesA,salesB); |
40 | } catch (FileNotFoundException e) { |
43 | } catch (UnsupportedEncodingException e) { |
46 | } catch (IOException e) { |
53 | } catch (IOException e) { |
Controller
6.controllerパッケージ内にIndex.javaを以下のように作成する。
●controller.Index.java
03 | import java.io.IOException; |
06 | import javax.servlet.RequestDispatcher; |
07 | import javax.servlet.ServletContext; |
08 | import javax.servlet.ServletException; |
09 | import javax.servlet.annotation.WebServlet; |
10 | import javax.servlet.http.HttpServlet; |
11 | import javax.servlet.http.HttpServletRequest; |
12 | import javax.servlet.http.HttpServletResponse; |
15 | import model.SalesParser; |
19 | public class Index extends HttpServlet { |
20 | private static final long serialVersionUID = 1L; |
22 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
23 | ServletContext application= this .getServletContext(); |
25 | List<Sales> list=(List<Sales>) application.getAttribute( "list" ); |
27 | String path=application.getRealPath( "/WEB-INF/data/sample.csv" ); |
28 | SalesParser parser= new SalesParser(); |
29 | list=parser.getList(path); |
30 | application.setAttribute( "list" ,list); |
33 | RequestDispatcher rd=request.getRequestDispatcher( "/WEB-INF/view/index.jsp" ); |
34 | rd.forward(request, response); |
View
7.WEB-INF内にviewフォルダーを作成し、index.jspを以下のように作成する。
●/WEB-INF/view/index.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" |
02 | pageEncoding= "UTF-8" import = "model.*,java.util.*" %> |
04 | List<Sales> list=(List<Sales>)application.getAttribute( "list" ); |
09 | <meta charset= "UTF-8" /> |
10 | <title>Insert title here</title> |
14 | <tr><th>年度</th><th>SalesA</th><th>SalesB</th></tr> |
16 | <tr><td><%=s.getYear() %></td><td><%=s.getSalesA() %></td><td><%=s.getSalesB() %></td><tr> |
コメント