質問編集履歴

3

ソースコードへ追記しました。

2016/09/06 02:19

投稿

itmsd
itmsd

スコア16

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,24 @@
36
36
 
37
37
  ###該当のソースコード
38
38
 
39
+
40
+
41
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42
+
43
+ doAction(request, response);
44
+
45
+ }
46
+
47
+
48
+
49
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50
+
51
+ doAction(request, response);
52
+
53
+ }
54
+
55
+
56
+
39
57
  private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40
58
 
41
59
  HttpSession session = request.getSession(true);

2

ソースコードを追加しました。

2016/09/06 02:19

投稿

itmsd
itmsd

スコア16

test CHANGED
File without changes
test CHANGED
@@ -35,6 +35,78 @@
35
35
 
36
36
 
37
37
  ###該当のソースコード
38
+
39
+ private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40
+
41
+ HttpSession session = request.getSession(true);
42
+
43
+ response.setHeader("Cache-Control", "no-cache");
44
+
45
+ response.setHeader("Pragma","no-cache");
46
+
47
+ request.setCharacterEncoding("UTF-8");
48
+
49
+
50
+
51
+ /*途中省略*/
52
+
53
+
54
+
55
+ synchronized (session) {
56
+
57
+ sc_ = getServletConfig().getServletContext();
58
+
59
+ connPool_ = (ConnectionPool)sc_.getAttribute("connPool");
60
+
61
+ loginUser = (SysUserBeans)session.getAttribute("loginUser");
62
+
63
+
64
+
65
+ String sceneStr = request.getParameter("scene");
66
+
67
+ int scene = -1;
68
+
69
+ if (sceneStr != null) scene = Integer.parseInt(sceneStr);
70
+
71
+ switch(scene) {
72
+
73
+ case 1: // 一覧
74
+
75
+ response.setContentType(CONTENT_TYPE);
76
+
77
+ showRows(request, response, session);
78
+
79
+ break;
80
+
81
+
82
+
83
+ /*途中省略*/
84
+
85
+
86
+
87
+ case 22: // CSV作成
88
+
89
+ downloadCSV(request, response, session);
90
+
91
+ break;
92
+
93
+
94
+
95
+ default: // 検索条件
96
+
97
+ response.setContentType(CONTENT_TYPE);
98
+
99
+ sc_.getRequestDispatcher("/view/sales/salesAggregateFrame.jsp").forward(request, response);
100
+
101
+ }
102
+
103
+ rows.setReqPrintUrl(null);
104
+
105
+ }
106
+
107
+ }
108
+
109
+
38
110
 
39
111
 
40
112
 
@@ -88,6 +160,8 @@
88
160
 
89
161
  ps.write(rows.getBalance());
90
162
 
163
+ ps.flush();
164
+
91
165
  }
92
166
 
93
167
  ps.close();

1

タイトルを具体的にし、とソースコードを簡潔にいたしました。

2016/09/06 01:04

投稿

itmsd
itmsd

スコア16

test CHANGED
@@ -1 +1 @@
1
- サーブレットから動的に作成したCSVをダウンロードさせたいと思います
1
+ サーブレットから動的に作成したCSVを遅延なく記録させたい。
test CHANGED
@@ -2,11 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- サーブレットでCSVを作成させて、作成が完了した際はダウンロードボタンを
5
+ サーブレットでCSVを作成させて遅延なくハードディスクへ記録させたいと思います。
6
-
7
- JSPで表示させて、それをクリックすると”開く”の選択肢が表示されますので、
8
-
9
- ”開く”をクリックしてExcelで表示させることを希望しております。
10
6
 
11
7
 
12
8
 
@@ -14,17 +10,9 @@
14
10
 
15
11
 
16
12
 
17
- サーブレットを実行し、作成直後表示されたダウンロードボタンをリック
13
+ サーブレットを実行し、CSVを作成するのですが、実際ードディスに記録
18
14
 
19
- すると、開けなかったり、一つ前に実行した内容だったりするのですが、
20
-
21
- ダウンロードボタンを5秒程度待ってからクリックしてやると問題ありせん
15
+ されるまでに5秒程度の遅延が発生して
22
-
23
-
24
-
25
- ソフト上は作成実行が終了しているのですが、実際にハードディスクに記録される
26
-
27
- のに時差があるようです。
28
16
 
29
17
 
30
18
 
@@ -36,7 +24,9 @@
36
24
 
37
25
 
38
26
 
27
+ ソースコード問題、tomcat等の設定でお気づきの点がございましたら、
28
+
39
- 対応策をご教示いただければ、幸甚にぞんじます。
29
+ ご教示いただければ、幸甚にぞんじます。
40
30
 
41
31
 
42
32
 
@@ -48,11 +38,19 @@
48
38
 
49
39
 
50
40
 
41
+ private void downloadCSV(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
42
+
43
+ SalesAggregateBeans rows = (SalesAggregateBeans)session.getAttribute("salesAggregate");
44
+
45
+ SysUserBeans loginUser = (SysUserBeans)session.getAttribute("loginUser");
46
+
47
+
48
+
51
49
  String fileName;
52
50
 
53
51
  fileName = sc_.getRealPath("/data/210.csv");
54
52
 
55
- rows.setReqPrintUrl(response.encodeURL("/example/data/210.csv"));
53
+ rows.setReqPrintUrl(response.encodeURL("/example/data/.csv"));
56
54
 
57
55
 
58
56
 
@@ -62,11 +60,9 @@
62
60
 
63
61
 
64
62
 
65
- String br = System.getProperty("line.separator");
63
+ int iMax = rows.getSize();
66
64
 
67
65
 
68
-
69
- int iMax = rows.getSize();
70
66
 
71
67
  for (int i = 0; i < iMax; i++) {
72
68
 
@@ -74,21 +70,13 @@
74
70
 
75
71
  ps.write("\"" + rows.getCd() + "\",\"");
76
72
 
77
- if (i < (iMax-1)) {
73
+ ps.write(rows.getName() + "\",\"");
78
74
 
79
- ps.write(rows.getName() + "\",\"");
75
+ ps.write(rows.getAddress() + "\",\"");
80
76
 
81
- ps.write(rows.getAddress() + "\",\"");
77
+ ps.write(rows.getReadings() + "\",\"");
82
78
 
83
- ps.write(rows.getReadings() + "\",\"");
84
-
85
- ps.write(rows.getSalesmanName() + "\",");
79
+ ps.write(rows.getSalesmanName() + "\",");
86
-
87
- } else {
88
-
89
- ps.write("*合計*\",,,,");
90
-
91
- }
92
80
 
93
81
  ps.write(rows.getCarryOver() + ",");
94
82
 
@@ -98,61 +86,29 @@
98
86
 
99
87
  ps.write(rows.getSum(2) + ",");
100
88
 
101
- if (rows.getReqTaxCalcFlag()) {
102
-
103
- ps.write(rows.getBalance() + ",");
104
-
105
- if (rows.getTaxDiv() == 1) {
106
-
107
- ps.write(rows.getNetTaxable() + ",");
108
-
109
- } else {
110
-
111
- ps.write("0,");
112
-
113
- }
114
-
115
- ps.write(rows.getSum(3) + br);
116
-
117
- } else {
118
-
119
- ps.write(rows.getBalance() + br);
89
+ ps.write(rows.getBalance());
120
-
121
- }
122
90
 
123
91
  }
124
-
125
- ps.flush();
126
92
 
127
93
  ps.close();
128
94
 
129
95
  ps = null;
130
96
 
131
- sc_.getRequestDispatcher("/view/sales/salesDownload.jsp").forward(request, response);
132
97
 
133
98
 
99
+ fo.close();
100
+
101
+ fo = null;
102
+
103
+ sc_.getRequestDispatcher("/view/sales/salesAggregate.jsp").forward(request, response);
104
+
105
+ }
134
106
 
135
107
  ###試したこと
136
108
 
137
109
 
138
110
 
139
- 上記コードの、ps=null;とsc_.getRequestDispatcher("/view/sales/salesDownload.jsp").forward・
140
-
141
- 間に下記コードを挿入して、ファイルがディスクに記録するのを待たせてしのでいます。
111
+ CSV出力件数はわずか5行でも、100行程度でも、5秒程度遅延が発生しています。
142
-
143
-
144
-
145
- try{
146
-
147
- // これで5秒スリープします。
148
-
149
- Thread.sleep(5000);
150
-
151
- } catch(InterruptedException e) {
152
-
153
- System.out.println(e.getMessage());
154
-
155
- }
156
112
 
157
113
 
158
114
 
@@ -165,3 +121,5 @@
165
121
  ・jdk8
166
122
 
167
123
  ・tomcat8
124
+
125
+ (サーブレットの設定は、インストール直後の状態です。)