株価csvファイルの読み書きを読みこんで表を作成してみよう。
●株価csvファイル
下からダウンロード
1 ファイル 303.12 KB
[実行例]
リクエストを投げると、以下のようなWEBページが表示される。
●●株価表の作成●●
1.エクリプス動的プロジェクトから[stockapp]を作成。
2.下から株価csvファイルをダウンロード
下からダウンロード
1 ファイル 303.12 KB
3.WEB-INF/の中にdataフォルダを作成し、そこにコピペでcsvファイルを貼り付ける。
4.modelパッケージを作成しその中にStock.java(Beans)を以下のように作成する。
●model.Stock.java
package model; public class Stock implements Serializable{ //コード,銘柄名,市場,始値,高値,安値,終値,出来高,売買代金 private String code,name,place,open,high,low,end,amount,sales; public Stock(){} public Stock(String code, String name, String place, String open, String high, String low, String end, String amount, String sales) { this.code = code; this.name = name; this.place = place; this.open = open; this.high = high; this.low = low; this.end = end; this.amount = amount; this.sales = sales; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getOpen() { return open; } public void setOpen(String open) { this.open = open; } public String getHigh() { return high; } public void setHigh(String high) { this.high = high; } public String getLow() { return low; } public void setLow(String low) { this.low = low; } public String getEnd() { return end; } public void setEnd(String end) { this.end = end; } public String getAmount() { return amount; } public void setAmount(String amount) { this.amount = amount; } public String getSales() { return sales; } public void setSales(String sales) { this.sales = sales; } }
5.modelパッケージ内にStockParser.javaを以下のように作成
●model.StockParser.java
package model; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class StockParser { public List<Stock> getList(String path){ List<Stock> list=new ArrayList<>(); BufferedReader br=null; try { FileInputStream fis=new FileInputStream(path); InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); br=new BufferedReader(isr); String line; while((line=br.readLine()) != null){ if(line.startsWith("コード")){ continue; } String[] vals=line.split(",",-1); list.add(new Stock(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7], vals[8])); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(br !=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return list; } }
6.controllerパッケージを作成し、その中にIndex.java(Servlet)を作成する。
●controller.Index.java
package controller; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.Stock; import model.StockParser; @WebServlet("/index.html") public class Index extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application=this.getServletContext(); List<Stock> list=(List<Stock>)application.getAttribute("list"); if(list==null){ String path=application.getRealPath("/WEB-INF/data/stocks_2017-08-16.csv"); StockParser parser=new StockParser(); list=parser.getList(path); application.setAttribute("list",list); } RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/index.jsp"); rd.forward(request, response); } }
7.WEB-INFの中にviewフォルダを作り、その中にindex.jspを作成する。
●index.jsp(WEB-INF/view/index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="model.*,java.util.*"%> <% List<Stock> list=(List<Stock>)application.getAttribute("list"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> <%if(list !=null && list.size() >0) {%> <table border="1"> <tr> <th>コード</th> <th>銘柄名</th> <th>市場</th> <th>始値</th> <th>高値</th> <th>安値</th> <th>終値</th> <th>出来高</th> <th>売買代金</th> </tr> <%for(Stock s:list){ %> <tr> <td><%=s.getCode() %></td> <td><%=s.getName() %></td> <td><%=s.getPlace() %></td> <td><%=s.getOpen() %></td> <td><%=s.getHigh() %></td> <td><%=s.getLow() %></td> <td><%=s.getEnd() %></td> <td><%=s.getAmount() %></td> <td><%=s.getSales() %></td> </tr> <%} %> </table> <%} %> </body> </html>
8.以下のように表示されれば成功だ。
[別解]
配列を使った解答例
〇model.Stock.java
package model; import java.io.Serializable; public class Stock implements Serializable{ private String[] data; public Stock() {}; public Stock(String[] data) { this.data=data; } public String[] getData() { return data; } public void setData(String[] data) { this.data = data; } }
〇model.StockParser.java
package model; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class StockParser { private List<String> headers; public List<Stock> getList(String path){ List<Stock> list=new ArrayList<>(); BufferedReader br=null; try { FileInputStream fis=new FileInputStream(path); InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); br=new BufferedReader(isr); String line; while((line=br.readLine())!=null) { String[] data=line.split(",",-1); if(line.startsWith("コード")) { this.headers=Arrays.asList(data); }else { Stock stock=new Stock(data); list.add(stock); } } } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(list.size()); return list; } public List<String> getHeaders() { return this.headers; } }
〇controller.Index.java
package controller; import java.io.IOException; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.Stock; import model.StockParser; @WebServlet("/Index") public class Index extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application=this.getServletContext(); List<Stock> list=(List<Stock>)application.getAttribute("list"); if(list ==null) { String path=application.getRealPath("/WEB-INF/data/stocks_2017-08-16.csv"); StockParser parser=new StockParser(); list=parser.getList(path); List<String> headers=parser.getHeaders(); application.setAttribute("headers", headers); application.setAttribute("list", list); } request.getRequestDispatcher("/WEB-INF/view/index.jsp").forward(request, response); } }
〇/WEB-INF/view/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="model.*,java.util.*"%> <% List<String> headers=(List<String>)application.getAttribute("headers"); List<Stock> list=(List<Stock>)application.getAttribute("list"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p>全<%=list.size() %>件</p> <%if(list != null && list.size()>0){ %> <table border="1"> <tr> <%for(String header:headers) {%> <th><%=header %></th> <%} %> <tr> <%for(Stock s:list){ %> <tr> <%for(String str:s.getData()){ %> <td><%=str %></td> <%} %> </tr> <%} %> </table> <%} %> </body> </html>
コメント