blog.Ring.idv.tw

Articles

Apache Tomcat 8 + SQLite

很長一段時間沒有更新該Blog了,由於最近為了做一些side project,所以打算在Mac筆電上架個簡單的開發環境,初步就想說直接利用SQLite來當做資料庫,反正只要中間層的DAO設計好,未來要改個後端也不是什麼太大的負擔,所以更不需要動到HBase之類的儲存方案。所以本文就純粹當作Memo...

必要軟體

.Apache Tomcat 8.5.43

為什麼選擇Tomcat 8的理由是,由於筆電的Java版本為Java 7,加上沒有急著升級的必要,所以就維持著...

至於SQLite的話,直接下載SQLite JDBC Driver即可,目前最新版本為sqlite-jdbc-3.27.2.1.jar

接下來就一步步地做些必要的操作:

修改 /etc/hosts

127.0.0.1    helloworld

這步驟主要是用來方便在筆電端快速地來做測試而已,上述的【helloworld】你可以改成你任意喜歡的,之後要測試時就直接在網頁上輸入http://helloworld:8080即可。

建立必要的目錄及檔案

$TOMCAT_HOME/helloworld/ROOT

$TOMCAT_HOME/helloworld/ROOT/index.jsp

上述index.jsp檔案裡任意寫個Hello JSP之類的文字即可。

修改 $TOMCAT_HOME/conf/server.xml

<Host name="helloworld"  appBase="helloworld"  unpackWARs="true" autoDeploy="true">
	     <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="helloworld_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
</Host>

啟動Tomcat

在啟動之前,如果你也是在Unix-like系統下安裝的話,那你可以考慮刪除掉$TOMCAT_HOME/bin/目錄底下的所有.bat檔案,這樣在透過Tab鍵補字操作下會方便些。

$TOMCAT_HOME/bin/startup.sh

接著在網頁上輸入http://helloworld:8080,應該就可以work了,接下來處理SQLite資料庫的部份。

建立必要的目錄及檔案

$TOMCAT_HOME/helloworld/ROOT/META-INF/context.xml

$TOMCAT_HOME/helloworld/ROOT/WEB-INF/classes

$TOMCAT_HOME/helloworld/ROOT/WEB-INF/lib

P.S. 請將sqlite-jdbc-3.27.2.1.jar直接copy到$TOMCAT_HOME/helloworld/ROOT/WEB-INF/lib/目錄下,至於其它目錄及檔案,待會再說明。

修改 META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="jdbc/helloworld" auth="Container" type="javax.sql.DataSource" driverClassName="org.sqlite.JDBC"
            url="jdbc:sqlite:/Users/yourname/apache-tomcat-8.5.43/helloworld/ROOT/helloworld.db"
            factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory">
  </Resource>
</Context>

上述的【url】用來設定SQLite的檔案位置,接著會透過下面的Servlet程式來操作該資料庫。

建立一個 Servlet 程式

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 javax.sql.DataSource;
import java.io.*;
import java.sql.*;

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		
		PrintWriter out = resp.getWriter();
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement ps = null;

		try
		{
			Context ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/helloworld");
			con =  ds.getConnection();

			Statement stat = con.createStatement();
			stat.executeUpdate("drop table if exists member;");
			stat.executeUpdate("create table member (id, name);");
			ps = con.prepareStatement("insert into member values (?, ?);");

			ps.setInt(1, 1);
			ps.setString(2, "Tony Lee");
			ps.addBatch();
			ps.setInt(1, 2);
			ps.setString(2, "Jack Wang");
			ps.addBatch();
			ps.executeBatch();


			rs = stat.executeQuery("select * from member;");
			while (rs.next()) {
				out.println("ID=" + rs.getInt("id")+"<br/>");
				out.println("Name=" + rs.getString("name")+"<br/>");
			}

		}catch (SQLException e)
		{
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();

			try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { ps.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { con.close(); } catch (SQLException e) { e.printStackTrace(); }
		}
	}
}

最後將上述程式編譯後,將HelloServlet.class檔案copy至$TOMCAT_HOME/helloworld/ROOT/WEB-INF/classes/目錄下。

重新啟動Tomcat,輸入網址http://helloworld:8080/HelloServlet就可以看到下述結果:

ID=1
Name=Tony Lee
ID=2
Name=Jack Wang

2019-07-24 19:49:43 | Add Comment

開始吧!

好久一陣子沒有上來寫文章了...Orz 自從菲律賓宿霧回來之後就都待在家陪陪親人,換個角度來說...過去這十年也就只有這半年「真正」在陪親人,所以上個月帶他們去體驗了一趟東京自由行! 沒錯...我的身份就是領隊... 從買機票、訂飯店、安排滑雪、迪士尼還有怎麼線上買「薯條三兄弟」都在我的責任範圍內...Orz 呵~ 雖然雜事很多,不過也蠻多收獲的,至少我的手機裡現在存有好幾個日本旅遊必備網站,因為難得經歷到東京大雪,所以當時我都在注意當地的天氣以安排行程,另一方面也要注意電車的狀況,因為真的讓我遇到電車停駛... 好在有及時掌握這些資訊,所以就算臨時變動行程也不至於會措手不及。

而現在...?其實我原先的打算是從東京回來之後...就準備再開始我的工作生涯,但是家人卻希望我留在中部,經過一些思考之後... 我暫時決定就先留在家中,當然我不可能每天無所事事地… 所以我決定先當個獨立開發者,花點時間玩玩自己想做的... 以後的事就再說吧! 畢竟年紀也不小了,還能有這樣的時間也不容易,就從這一篇文章開始出發吧!!

P.S. 想在網路上訂購「薯條三兄弟」的朋友,請到 Calbee官網 訂購,不僅有限量更要抓準時間搶 (日本快台灣一個小時)

2014-03-08 21:29:56 | Add Comment

Bohol island

About ten days ago, I went to Bohol Island to see the famous tarsiers and Chocolate Hills, and of course, it was my first time there. During this short trip, I learned that the tarsier is one of the smallest primates in the world, about the size of a human palm. Unfortunately, as far as I know, their lifespan is only about twenty to twenty-five years, and their main food is insects, especially spiders. Another famous attraction is the Chocolate Hills. They were quite new to me because I had no idea at all how they were formed.

Aside from that, I also experienced dolphin watching and snorkeling. I also visited Virgin Island. I must say, if you want to enjoy snorkeling, you should go to Balicasag because you can see many tropical fish there, even sea turtles. And please don’t forget to put on some sunblock; otherwise, you might experience what I did. :)

2013-06-11 19:44:19 | Comments (1)

What is next?

Recently, I went to the Philippines alone to take a break and have a vacation. Since I’m not getting any younger, I needed some time to think about what’s next. More precisely, I have no idea what I want at the moment, but I clearly know what I don’t want. As I mentioned above, I chose to go to the Philippines even though there have been many disputes between Taiwan and the Philippines recently.

2013-05-27 22:56:52 | Comments (1)

IMG2WebP.net is Announced!

WebP圖檔格式(based on VP8 codec)最初是由Google在2010年9月底所發表的:WebP, a new image format for the Web,它是一種支援無損(lossless)壓縮和有損(lossy)壓縮的圖檔格式,根據Google的官方測試說明.. WebP無損壓縮的圖檔格式和PNG檔相較之下,它能比PNG檔還少26%的檔案大小;若是採用有損壓縮的話也能比JPEG圖檔再少25~34%的檔案大小,話雖如此~ 但是這畢竟是較新的圖檔格式,所以現階段有支援WebP的瀏覽器也比較少~ 除了Google本身的Chrome之外,就只有Opera有支援(CSS gradients, WebP, and Declarative UI),不過如果現在想試用看看的話,除了可以到WebP官網下載安裝之外,還可以透過IMG2WebP.net這個線上轉檔工具,直接拖張圖片進去就可自動轉成WebP圖檔,所以筆者的年假就弄了這個東西來練練功...Orz 希望有朝一日WebP圖檔的涵蓋率能佔有一席之地。

IMG2WebP線上轉檔工具

IMG2WebP.net

相關資源

A Comprehensive Guide to WebP

2013-02-19 15:35:39 | Add Comment

Previous Posts
Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment