<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JSP &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/jsp/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 18 Apr 2023 08:16:56 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>JSP &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS</title>
		<link>https://noter.tw/8538/err_cache_miss/</link>
					<comments>https://noter.tw/8538/err_cache_miss/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sat, 05 Jun 2021 06:41:57 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[網頁前端]]></category>
		<category><![CDATA[ERR_CACHE_MISS]]></category>
		<category><![CDATA[history.back()]]></category>
		<category><![CDATA[送出表單]]></category>
		<category><![CDATA[重新提交表單]]></category>
		<category><![CDATA[上一頁]]></category>
		<category><![CDATA[文件已過期]]></category>
		<category><![CDATA[重新導向]]></category>
		<category><![CDATA[Cache-Control]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[JSP]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=8538</guid>

					<description><![CDATA[<p>原本可以正常使用的系統裡，經過資安調整之後出現了 ERR_CACHE_MISS 的問題，記錄一下解決的方法。 平常瀏覽網頁的時候也會遇到，當送出表單後，點選「上一頁」常可以看到文件已過期，或是重新傳送&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/8538/err_cache_miss/" data-wpel-link="internal">Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>原本可以正常使用的系統裡，經過資安調整之後出現了 ERR_CACHE_MISS 的問題，記錄一下解決的方法。</p>



<span id="more-8538"></span>



<p class="withcode">平常瀏覽網頁的時候也會遇到，當送出表單後，點選「上一頁」常可以看到文件已過期，或是重新傳送這些資料才可以正確顯示之類的錯誤訊息，記得以前會看到<code>ERR_CACHE_MISS</code>錯誤的字樣，小蛙這次遇到的只有顯示如下圖</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/ACtC-3eSvPTbFIORBjFcpEsPvn90wGzIaXNUCrp25i3s9QSent9yf6064q3zx3R1JpHozzATD2LhTwU1_d3al4waPQ5RX7JAmDlMN2Nf7eo-xfyuYanrDOQk5Aw-T6GKADAvVyk6SclvZGi7YVpD79o8o_lA7Q=w1314-h650-no?authuser=2" alt="ACtC 3eSvPTbFIORBjFcpEsPvn90wGzIaXNUCrp25i3s9QSent9yf6064q3zx3R1JpHozzATD2LhTwU1 d3al4waPQ5RX7JAmDlMN2Nf7eo xfyuYanrDOQk5Aw T6GKADAvVyk6SclvZGi7YVpD79o8o lA7Q=w1314 h650 no?authuser=2 Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS" title="Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS"><figcaption>Chrome 錯誤訊息：這個網頁需要使用你先前輸入的資料才能正確顯示。你可以重新傳送這些資料，不過這麼做會重複執行這個網頁先前執行過的任何動作。 (ERR_CACHE_MISS)</figcaption></figure>



<figure class="wp-block-image size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/ACtC-3f_qjJrM_VT4J6DVmyBUA8UnC4f3AzMssF_Ctxux1nsRzdsxZXI0prLeuH-01YNoQ3WYgIpG-BzUYaeDt3unvCruWrxGTIOnzNOcr3eynoV4gRC84NUQCEhzWcUTyRtEdBwsgZQn02TmqZD1j9-lkS8Cg=w826-h494-no?authuser=2" alt="ACtC 3f qjJrM VT4J6DVmyBUA8UnC4f3AzMssF Ctxux1nsRzdsxZXI0prLeuH 01YNoQ3WYgIpG BzUYaeDt3unvCruWrxGTIOnzNOcr3eynoV4gRC84NUQCEhzWcUTyRtEdBwsgZQn02TmqZD1j9 lkS8Cg=w826 h494 no?authuser=2 Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS" title="Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS"><figcaption>Firefox 錯誤訊息：文件已過期 此文件已不存在。您所請求的文件已不存在於 Firefox 的快取當中。為了您的安全，Firefox 將不會自動重新請求敏感文件。請點下重試以重新向網站請求取得文件。(ERR_CACHE_MISS)</figcaption></figure>



<p class="withcode">小蛙接手處理的這個系統則是原本點選上一頁沒有問題，經過資安掃描修正後發生的，Google 及跟處理過該狀況的同事討論過之後找到相同的答案，如果在 header 中加入<code>Cache-Control: private</code>的設定，就可以解決掉這個問題！</p>



<h2 class="para wp-block-heading">PHP</h2>



<pre class="wp-block-preformatted withcode">// 在 session_start(); 後加入
header("Cache-Control:private");</pre>



<h2 class="para wp-block-heading">JSP</h2>



<pre class="wp-block-preformatted withcode">response.setHeader("Cache-Control", "private");</pre>



<p>參考資料：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>Chrome浏览器对于POST页面执行history.back返回或表单数据丢失的解决办法<br>(連結失效 https://www.unixso.com/Php/post-header.html)</li><li><a href="https://blog.csdn.net/A9925/article/details/42027229" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">ERR_CACHE_MISS 搜尋回上一頁空白或錯誤<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li><a href="https://www.cnblogs.com/houdj/p/9685571.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">ERR_CACHE_MISS 上一页提示确认重新提交表单<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/8538/err_cache_miss/" data-wpel-link="internal">Post 後上一頁 history.back() 發生文件已到期/認重新提交表單 ERR_CACHE_MISS</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/8538/err_cache_miss/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>中文亂碼總整理 (Tomcat 5.5)</title>
		<link>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/</link>
					<comments>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:21:55 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[中文亂碼]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[charset]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2902</guid>

					<description><![CDATA[<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。 其實解決中文&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。</p>



<span id="more-2902"></span>



<p>其實解決中文亂碼，只要把所有的編碼格式都調整成一致，比如說，編輯器預設儲存編碼為 Big5，卻把頁面設定成 <code>&lt;%@ page contentType="text/html; charset=utf-8" %></code>，這樣一來，在瀏覽器開啟的網頁，中文就會是亂碼。(反過來也會是亂碼)。唯有兩者設定都是相同編碼的時候，在網頁上才能正確顯示中文。</p>



<p>上面提到，把所有的編碼都設成 Big5，這麼一來，也符合了「所有編碼都設為相同」的原則，當然包括編輯器預設儲存編碼也是 Big5，如此網頁可以正確顯示中文字，但是 &#8230; 如果後端需要加上 MySQL 資料庫，如何在表單跟 MySQL 之間正確傳遞中文，可能又會是一個花時間的問題，經過幾番折騰之後，決定把所有的編碼都設定為 utf-8 比較方便。</p>


<div id="rank-math-howto" class="rank-math-block" >
<div class="rank-math-howto-description">

</div>

<div class="rank-math-steps ">
<div id="howto-step-643e51f888560" class="rank-math-step">
<h3 class="rank-math-step-title ">編輯器預設儲存編碼 => 設定成 <strong>UTF-8</strong></h3>
<div class="rank-math-step-content "><p>以 EditPlus3 為例，可從「工具」->「偏好項」->「檔案」->「預設編碼模式」改成 UTF-8，<br />注意之前可能存成 Big5 的，可以按另存新檔後，最下面編碼模式可以改變。</p>
</div>
</div>
<div id="howto-step-643e51f888562" class="rank-math-step">
<h3 class="rank-math-step-title ">每個 JSP 頁面 charset 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. &lt;%@ page contentType=&#8221;text/html; charset=utf-8&#8243; %></p>
</div>
</div>
<div id="howto-step-643e51f888563" class="rank-math-step">
<h3 class="rank-math-step-title ">MySQL my.ini 設定成</h3>
<div class="rank-math-step-content ">[client]default-character-set=utf8<br />[mysqld]default-character-set=utf8</p>
</div>
</div>
<div id="howto-step-643e51f888564" class="rank-math-step">
<h3 class="rank-math-step-title ">資料庫、資料表及欄位的 <strong>character-set</strong> 設為 utf8，<strong>collation</strong>(校對)設定為utf8_unicode_ci。</h3>
<div class="rank-math-step-content "></div>
</div>
<div id="howto-step-643e51f888565" class="rank-math-step">
<h3 class="rank-math-step-title "><strong>資料庫連結部份</strong>修改</h3>
<div class="rank-math-step-content "><p>Connection con=DriverManager.getConnection(&#8220;jdbc:mysql://localhost/user?useUnicode=true&amp;characterEncoding=utf8&#8221;, &#8220;帳號&#8221;, &#8220;密碼&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888566" class="rank-math-step">
<h3 class="rank-math-step-title ">Servlet 部份同樣把 <strong>charset</strong> 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. request.setContentType(&#8220;text/html; charset=utf-8&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888567" class="rank-math-step">
<h3 class="rank-math-step-title ">GET 亂碼解決</h3>
<div class="rank-math-step-content "><p>在 %TOMCAT_HOME%\conf\server.xml 中，找到&lt;connector port=&#8221;8080&#8243; &#8230; 部分，加上 URIEncoding = &#8220;UTF-8&#8243;，重啟 Tomcat。</p>
</div>
</div>
<div id="howto-step-643e51f888568" class="rank-math-step">
<h3 class="rank-math-step-title ">POST 亂碼解決</h3>
<div class="rank-math-step-content "><p>若是有多個網站，將 %TOMCAT_HOME%\webapps\jsp-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class 複製到%TOMCAT_HOME%\shared\classes\filters 下，(若沒有資料夾可以自己新增)，修改 %TOMCAT_HOME%\webapps\(mytest)\WEB-INF\web.xml (mytest 為網站目錄)，加上以下內容 </p>
</div>
</div>
</div>
</div>


<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;filter> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;filter-class>filters.SetCharacterEncodingFilter&lt;/filter-class> 
  &lt;init-param> 
    &lt;param-name>encoding&lt;/param-name> 
    &lt;param-value>Big5&lt;/param-value> 
  &lt;/init-param> 
&lt;/filter> 
&lt;filter-mapping> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;url-pattern>/*&lt;/url-pattern> 
&lt;/filter-mapping></pre>



<p>照著這些步驟做，應該就能有個能正確顯示中文的平台(或許吧！至少小蛙現在的環境就是)如果發現什麼問題，再隨時補上。</p>



<p><strong>2009/07/31 補充：<br></strong>在使用 Prototype.js 的 Updater / Requester function 時，編碼為 big5 傳遞中文會發生亂碼，<br>PHP 的解決方式 -> <code>header("Content-Type:text/html;charset=BIG5");</code><br>JSP 解決方式 ->  <code>response.setHeader("Charset","BIG5"); </code><br>這裡 AJAX 的傳遞方式要用 get。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JSP 連結 MySQL</title>
		<link>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/</link>
					<comments>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:18:16 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[connector]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[連結資料庫]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2889</guid>

					<description><![CDATA[<p>Java 連結 MySQL 的方法 記錄用Java連結MySQL資料庫的方法，這篇要介紹JSP如何連結MySQL。這篇文章是從舊部落格移到這邊記錄留存。步驟：安裝MySQL =&#62; 安裝ODBC &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://noter.tw/2883" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Java 連結 MySQL 的方法</a> 記錄用Java連結MySQL資料庫的方法，這篇要介紹JSP如何連結MySQL。這篇文章是從舊部落格移到這邊記錄留存。<br /><span id="more-2889"></span><br />步驟：安裝MySQL =&gt; 安裝ODBC =&gt; 安裝JDBC。</p>
<ul>
<li>
<h2>安裝 MySQL</h2>
<ul>
<li>進入 MySQL 下載頁面，http://dev.mysql.com/downloads/mysql/5.0.html。</li>
<li>依自己的平台選擇下載類型，若是 Win 環境，可下載 Windows Essentials 安裝檔或是不包含安裝檔的 Without installer 這項。</li>
<li>下載完之後，擊點兩下開始安裝。(期間會有些 MySQL Server 的設定)</li>
</ul>
</li>
<li>
<h2>安裝 ODBC</h2>
<ul>
<li>下載 ODBC，http://dev.mysql.com/downloads/connector/odbc/5.1.html。</li>
<li>安裝 ODBC。</li>
<li>安裝完成後，開啟 &#8220;控制台&#8221; -&gt; &#8220;系統管理工具&#8221; -&gt; &#8220;資料來源(ODBC)&#8221;，之後切換到 &#8220;系統資料來源名稱&#8221; 頁面下，看是否有 &#8220;MySQL&#8221;，有的話表示安裝成功。</li>
<li>沒有的話，選擇右邊的 &#8220;新增&#8221; -&gt; &#8220;MySQL ODBC 5.1 Driver&#8221; -&gt; &#8220;完成&#8221;，到這裡會跳出一個 MySQL 設定的視窗，&#8221;Data Source Name&#8221; 隨便輸入，下面包括 &#8220;Server/Port/User/Password&#8221; 都依照安裝 MySQL 時的設定輸入，之後按下 &#8220;Test&#8221;，出現 &#8220;Connection Successful&#8221; 表示整個安裝已經完成了。</li>
</ul>
</li>
<li>
<h2>安裝 JDBC</h2>
<ul>
<li>下載 JDBC，http://dev.mysql.com/downloads/connector/j/5.1.html。</li>
<li>下載完成後解壓縮，找到裡面有個檔名是 &#8220;mysql-connector-java-5.1.6-bin.jar&#8221; 的資料，複製到 Tomcat 安裝目錄下的 &#8220;\shared\lib&#8221; 下。</li>
<li>設計 JSP 文件時，用以下指令就可以連結資料庫。
<pre class="brush: java; gutter: true">String JDBCDriver = "com.mysql.jdbc.Driver";
String strUrl = "jdbc:mysql://localhost:3306/test?user=root&amp;characterEncoding=big5"
Class.forName(JDBCDriver);
Connection con = DriverManager.getConnection(strUrl);</pre>
</li>
</ul>
</li>
</ul>
<p><strong>參考資料：</strong></p>
<ul>
<li style="list-style-type: none;">
<ol>
<li><a href="https://noter.tw/2883" data-wpel-link="internal">Java 連結 MySQL</a></li>
<li><a href="https://noter.tw/1988" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>
</ol>
</li>
</ul>


<p><strong>MySQL / MariaDB / Oracle 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list">
<li><a href="https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/" data-wpel-link="internal">MariaDB JSON_EXTRACT: 從 JSON 欄位取值</a></li>



<li><a href="https://noter.tw/12015/oracle-%e5%88%86%e7%b5%84%e7%b7%a8%e8%99%9f%e8%88%87%e5%85%a8%e6%8e%92%e5%ba%8f%e7%b7%a8%e8%99%9f-row_number-over-partition-by/" data-wpel-link="internal">Oracle, MySQL 分組編號與全排序編號 row_number, over, partition by</a></li>



<li><a href="https://noter.tw/3929/mysql-error-2003-hy000-%E7%84%A1%E6%B3%95%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A/" data-wpel-link="internal">MySQL ERROR 2003 (HY000) 無法遠端連線</a></li>



<li><a href="https://noter.tw/2889/jsp-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a></li>



<li><a href="https://noter.tw/2883/java-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a></li>



<li><a href="https://noter.tw/2306/mysql%E6%8E%92%E7%A8%8Bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a></li>



<li><a href="https://noter.tw/1988/java%E9%80%A3%E7%B5%90%E9%81%A0%E7%AB%AFmysql/" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>



<li><a href="https://noter.tw/4508/mariadb-replication-%E8%A8%AD%E5%AE%9A/" data-wpel-link="internal">MariaDB Replication 設定</a></li>



<li><a href="https://noter.tw/122/java-php-%E6%8F%92%E5%85%A5-emoji-%E8%B3%87%E6%96%99%E5%88%B0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a></li>
</ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JSP將資料匯出成Excel直接下載 by JExcelApi (jxl)</title>
		<link>https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/</link>
					<comments>https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 28 Dec 2011 15:42:01 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[JExcelApi]]></category>
		<category><![CDATA[jxl]]></category>
		<category><![CDATA[excel下載]]></category>
		<category><![CDATA[html to excel]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=1732</guid>

					<description><![CDATA[<p>這篇文章小蛙要記錄如何在JSP中使用JExcelApi(jxl)操作Excel並且提供下載的功能，例如查詢完的列表讓使用者可以直接下載存成Excel。小蛙原本寫了一種透過File的方法產生，不過缺點就&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/" data-wpel-link="internal">JSP將資料匯出成Excel直接下載 by JExcelApi (jxl)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇文章小蛙要記錄如何在JSP中使用JExcelApi(jxl)操作Excel並且提供下載的功能，例如查詢完的列表讓使用者可以直接下載存成Excel。小蛙原本寫了一種透過File的方法產生，不過缺點就是時間久了會有很多廢棄的檔案(因為每次下載都會存成一個xls)，必須透過crontab或是手動定期清理。之後在<a rel="noreferrer noopener nofollow external" href="http://blog.xuite.net/syang.phon/photobox/19700170" target="_blank" data-wpel-link="external" class="wpel-icon-right"><strong>Jsp利用jxl匯出資料至Excel並下載 @ Ken™ KM</strong><span class="wpel-icon wpel-image wpel-icon-6"></span></a>看到更好的方法，可以動態產生並且直接提供下載，也就不會有上述的問題了。</p>



<span id="more-1732"></span>



<p><a rel="noreferrer noopener nofollow external" href="http://wazai.net/1569/java-excel-jxl" target="_blank" data-wpel-link="external" class="wpel-icon-right">Java + Excel = JXL<span class="wpel-icon wpel-image wpel-icon-6"></span></a>小蛙在這篇文章中記錄了在Java中如何透過jxl讀取Excel檔案，這篇文章則是記錄如何寫入Excel檔案。會出現這篇文章完全是一個意外，原本這個工作是由另一位同事負責，結果那位同事連續請假兩天，而恰好小蛙是他的組長，另一個原因是，晚上快六點的時候，小蛙的組長才跟小蛙說隔天中午前要處理好，下午對方要做驗收的動作，所以小蛙只好回到家繼續趕工。累了，不囉嗦直接進正題，老規矩只記錄用到的部份，對jxl有興趣的網友們，請見諒，可能要自行查找jxl document喔！</p>



<h2 class="para wp-block-heading">操作寫入Excel檔案(不含直接下載)</h2>



<p>這邊必須先有一個 WritableWorkbook 物件公我們操作(讀取的時候我們使用的是 Workbook 物件)，接著是 WritableSheet(讀取的時候是 Sheet)，建立完 WritableWorkbook 及 WritableSheet 之後就開始一個 cell 一個 cell 塞資料，這邊小蛙只有用到字串類型的文字，所以只採用 Label 即可。</p>


<pre>&lt;%@ page language="java" contentType="text/html; charset=utf-8" %&gt;
&lt;%@ page import="java.util.*" %&gt;
&lt;%@ page import="java.io.*" %&gt;
&lt;%@ page import="java.text.*" %&gt;
&lt;%@ page import="jxl.*" %&gt;
&lt;%@ page import="jxl.write.*" %&gt;
&lt;%@ page import="jxl.write.biff.RowsExceededException" %&gt;
&lt;%!
// 參考資料 http://www.blogjava.net/chenlb/archive/2007/10/29/156613.html
private void putRow(WritableSheet ws, int rowNum, ArrayList cells) throws RowsExceededException, WriteException {
    for(int j=0; j &lt; cells.size(); j++) {
      // 建立每個cell物件，這邊只用了Label
      Label cell = new Label(j, rowNum, ""+cells.get(j));
        // 塞入Cell中
        ws.addCell(cell);
      }
}
%&gt;
&lt;%
File f = null;
try{
    // 只是用來產生檔名
    long sysTime = System.currentTimeMillis();
    java.util.Date date = new java.util.Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String dateString = sdf.format(date) + sysTime + ".xls";
    String path = application.getRealPath("");
    f = new File(path + "/upload/" + dateString);
    if(!f.exists()){
        // 建立excel檔案(空檔案，供jxl操作)
        f.createNewFile();
    }
    WritableWorkbook workbook = Workbook.createWorkbook(f);
    WritableSheet sheet = workbook.createSheet("Sheet1", 0);
    int rowNum = 0;
    // ... 資料庫讀取部份略過 ...
    while(rs.next()){
        // 建立一個ArrayList儲存每個Record的資料
        ArrayList list = new ArrayList();
        list.add(rs.getString("name");
        ...
        // 實際塞資料
        putRow(sheet, rowNum, list);
        // 用來控制行數
        rowNum++;
    }
    // 這兩行一定不能少，不然寫的資料會消失
    workbook.write();
    workbook.close();
}catch(Exception e){
  out.print(e);
}
%&gt;</pre>
<p> </p>
<ul>
<li><span style="color: #ff0000;"><strong>操作寫入Excel並直接下載</strong></span></li>
</ul>
<p style="padding-left: 30px;">這邊是透過將Excel直接輸出到OutputStream，再透過response.setHeader達到下載的目的，直接看code。</p>
<pre>&lt;%@ page language="java" contentType="text/html; charset=utf-8" %&gt;
&lt;%@ page import="java.util.*" %&gt;
&lt;%@ page import="java.io.*" %&gt;
&lt;%@ page import="jxl.*" %&gt;
&lt;%@ page import="jxl.write.*" %&gt;
&lt;%@ page import="jxl.write.biff.RowsExceededException" %&gt;
&lt;%!
    private void putRow(WritableSheet ws, int rowNum, ArrayList cells) throws RowsExceededException, WriteException {
        for(int j=0; j&amp;lt;cells.size(); j++) {
            Label cell = new Label(j, rowNum, ""+cells.get(j));
            ws.addCell(cell);
        }
    }
%&gt;
&lt;%
try{
    // 直接下載最重要的兩行
    response.reset();     
    response.setHeader("Content-disposition","attachment; filename=fileName.xls");
    OutputStream os = response.getOutputStream();
    WritableWorkbook workbook = Workbook.createWorkbook(os);
    WritableSheet sheet = workbook.createSheet("Sheet1", 0);
    // ... 資料庫操作省略 ...
    int rowNum = 0;
    while(rs.next()){
        ArrayList list = new ArrayList();
        list.add(rs.getString("name"));
        ....
        putRow(sheet, rowNum, list);
        rowNum++;
    }
    workbook.write();
    workbook.close();
    os.flush();
    os.close();
}catch(Exception e){
    out.print(e);
}
%&gt;</pre>


<p><strong>Excel 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/9117/excel-filter-and-validation/" data-wpel-link="internal">加入篩選器及下拉選單 (資料驗證)</a></li><li><a href="https://noter.tw/9063/excel-auto-search-and-fill-data-by-vlookup-index-match/" data-wpel-link="internal">搜尋表格 填入資料 強大的 VLOOKUP, INDEX, MATCH</a></li><li><a href="https://noter.tw/9061/split-a-excel-to-files/" data-wpel-link="internal">分拆 Excel 成多個檔案 ( Split A Excel to Files)</a></li><li><a href="https://noter.tw/6550/excel-%e4%bd%bf%e7%94%a8-poi-%e8%ae%80%e5%8f%96%e6%96%87%e5%ad%97%e6%a0%bc%e5%bc%8f%e6%97%a5%e6%9c%9f%e5%8d%bb%e8%ae%8a%e6%88%90%e6%95%b8%e5%ad%97%e7%9a%84%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">Excel 使用 POI 讀取文字格式日期卻變成數字的問題</a></li><li><a href="https://noter.tw/4300/java-%e8%ae%80%e5%8f%96-excel-%e6%96%87%e4%bb%b6xls-xlsx-%e4%bd%bf%e7%94%a8-apache-poi/" data-wpel-link="internal">Java 讀取 Excel 文件(xls, xlsx) – 使用 Apache POI</a></li><li><a href="https://noter.tw/2711/oracle-%e5%8c%af%e5%85%a5%e5%8c%af%e5%87%ba-excel-%e9%80%8f%e9%81%8e-sql-developer/" data-wpel-link="internal">Oracle 匯入/匯出 Excel (透過 SQL Developer)</a></li><li><a href="https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/" data-wpel-link="internal">JSP將資料匯出成Excel直接下載 by JExcelApi (jxl)</a></li><li><a href="https://noter.tw/1569/java-excel-jxl/" data-wpel-link="internal">Java + Excel = JXL</a></li><li><a href="https://noter.tw/119/ncr-%e8%99%95%e7%90%86%e6%96%b9%e5%bc%8f%ef%bc%9ajava-vba/" data-wpel-link="internal">NCR &amp;#xxxxx; 處理方式：Java &amp; VBA</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/" data-wpel-link="internal">JSP將資料匯出成Excel直接下載 by JExcelApi (jxl)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
