2007年11月26日 星期一

Web Server Setup(Ubuntu 7.10 + Tomcat 6 + MySQL + JDBC)

這幾天從家裡整裡了一台電腦,架設一台Web Server,以後會慢慢將資料轉移到這台電腦上。

規格為:
Intel Celeron 1000MHZ
512M RAM
Ubuntu 7.10
Tomcat 6.0.14
在安裝Ubuntu 7.10完後,發現並沒有安裝ssh的服務,準備apt-get時,竟然找不到相關套件,最後發現原來/etc/apt/source.list中所有的更新網站列表都被註解了,將所有#刪掉後,開始安裝openssh-server.
sudo apt-cache update
sudo apt-get install ssh
安裝完成後的ssh預設port為22,可以從/etc/ssh/sshd_config下將port改成另一個port,防止被攻擊。

Tomcat 6的安裝步驟:
1. sudo apt-get install sun-java6-jdk (取得java sdk環境)
2. sudo update-alternatives --config java (設定預設運行的java環境,選擇上一步安裝的java6)
3. 下載tomcat 6(連結)
4. sudo mv apache-tomcat-6.0.14.tar.gz /opt/ (將tomcat移至/opt/下,可自行選擇)
5. sudo tar zxvf apache-tomcat-6.0.14.tar.gz (將目錄改為tomcat6)
6. sudo vim /etc/profile (也可以加在家目錄下的.bashrc)
└ 加入此敘述export JAVA_HOME=/usr/lib/jvm/java-6-sun
7. 登出再登入
8. sudo /opt/tomcat6/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat6
Using CATALINA_HOME: /opt/tomcat6
Using CATALINA_TMPDIR: /opt/tomcat6/temp
Using JRE_HOME: /usr/lib/jvm/java-6-sun
(成功訊息)
9. 從/opt/tomcat6/conf/server.xml中修改port(從8080改為80)
10.
sudo /opt/tomcat6/bin/shutdown.sh
11.
sudo /opt/tomcat6/bin/startup.sh
12. 連線到http://your-ip/
安裝完成後,預設的web存放位置為/opt/tomcat6/webapps/ROOT/,如需要改變預設路徑可透過/opt/tomcat6/conf/server.xml此檔改變。找到Host name並更改,如下~
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="ROOT";
crossContext="true">
</Context>
</Host>
改為:
<Host name="localhost" appBase="/home/username/www/"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase=".";
crossContext="true">
</Context>
</Host>
安裝MySQL 5.0
sudo apt-get install mysql-server (安裝完成後,會要求輸入password)
安裝JDBC
1. sudo apt-get install libmysql-java
2. sudo cp ./usr/share/java/mysql-connector-java-5.0.4.jar /opt/tomcat6/lib/ (粗體部份為Tomcat安裝路徑)
(重新啟動Tomcat將jdbc lib匯入)
3.
sudo /opt/tomcat6/bin/shutdown.sh
4.
sudo /opt/tomcat6/bin/startup.sh
測試JDBC(將下敘述加至*.jsp網頁中)
1.連線到mysql command
└ sudo mysql -p (-p為所輸入之root密碼)
2. 建立test資料庫
└ create database test;
3. show database
└ show databases;
4. 建立sa使用者,密碼為sa,權限為SELECT
└ GRANT SELECT ON *.* TO sa@localhost IDENTIFIED BY "sa";
5. 撰寫測試網頁
<%@ page contentType="text/html;charset=big5" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<% String DB_URL = "jdbc:mysql:///test"; String DB_User = "sa"; String DB_Password = "sa"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection(DB_URL, "sa", "sa"); %>
if(!con.isClosed())
System.out.println("Successfully connected !!" );
6. 連線到網頁進行測試。

2007年11月22日 星期四

Naming Conventions for Symbian

最近比賽告一段落了,要開始學習Symbian的學習,發現....還蠻多東西要看的,像是基本的Coding conventions更為重要啊。以下是在Symbain上Coding時需要注意的convention:

(此資料轉載於Blue119's blog)

Function Convention

  • Trailing "D" indicates the deletion of an object
  • Trailing "L" means function may leave
  • Trailing "C" means an item is placed on the cleanup stack

-------------------------------------------

Variable Convention

  • Member variables' names begin with 'i'
  • Arguments' names begin with 'a'
  • Automatics' (local variables) names have no initial letter, but start in lower case
  • Constants' names begin with 'K'
  • Global variables are usually avoided, but when used, their names begin with a capital letter.

-----------------------------------

Class Convention

'T' Classes

  • Classes that don't own external objects/resources and so can be declared on the stack 'T'types contain their value.
  • They do not own any external object, either directly (by pointer) or indirectly (by handle).
  • they do not need a destructor to cleanup resources.
  • 'T' types may be allocated either on the stack (that is locally as C++ automatic variables) or as members of other classes.
  • note that the default stack size is 8KB in Symbian OS.
  • Structure types mostly begin with 'T'.
'C' Classes
If a class needs to dynamically allocate any memory or own another class, it should derive, directly or indirectly, from CBase (a built-in Symbian OS class), and its class name should begin with 'C'. CBase-derived classes have the following properties:
  • They are allocated on the heap (dynamically allocated) — not on the stack, and not as members of other classes.
  • The allocator used for this class hierarchy initializes all member data to binary zeroes.
  • They are passed by pointer, or reference, and so do not need an explicit copy constructor or assignment operator unless there is clear intention that a particular class supports copying.
  • They have a virtual destructor, which is used for standard cleanup processing.
  • They support two-phased construction – this will be covered later in the section.
'R' Classes
These classes are used to access system resources, for example files, via handles. The following are characteristics of ‘R’ classes:
  • They contain a handle that is used to pass on requests to other objects.
  • They are opened using an "Open()" function particular to the ‘R’ class, and closed using a "Close()" function particular to the class. An ‘R’ object must be closed once if it has been opened.
  • They may be freely bit-wise copied.
  • They have no explicit constructor, destructor, copy constructor or assignment operator.
'M' Classes
Characteristic:
  • Abstract
  • Pure virtual functions
  • No member data
Purpose:
  • define an interface
Advantage:
  • reduce dependencies between classes
Rule:
  • the only use of multiple inheritance
  • A C class derive from one other C class and zero or more M classes
'M'classes have the following restrictions:
  • They should contain no member data.
  • They should not contain constructors or destructors, or overloaded operators.
'M' classes usually contain pure virtual functions that define a fully abstract interface.

轉載自Blue119's blog:Symbian Coding Conventions:Class

2007年11月16日 星期五

Applet資源取用問題

很久沒有寫Applet了,上次撰寫相關的程式應該是大學的時候吧!(約...三年前~)

最近因為比賽的關係,需要使用到Applet,只是沒想到...竟然無法在網頁上執行(使用appletviewer可以正常執行)。在網路上找尋相關的資料,發現原來我的程式有使用內部資源(像是圖片...),而Applet因為安全性的關係,需要做簽章(Signature)的動作才可以使用。

簽章方式:
1. 安裝JDK(這樣才會有keytool、jarsigner工具)
2. 產生jar
└ jar cvf file.jar test.class ※file.jar為產生jar檔案名稱
3. 利用keytool產生key(*.crt)
keytool -genkey -alias appletkey ※輸入簽章相關資訊(密碼、姓名...等)
keytool -export -alias appletkey -file appletkey.crt ※產生crt檔,檔名為apu.crt
4. 將jar進行簽證
jarsigner file.jar appletkey ※會要求輸入剛剛簽章時所用的密碼
5. 轉換html
htmlconverter
以上步驟1、3只需要做一次就可以,之後就重覆2、4步驟即可以。

簽章完成後,發現還是無法正確使用資源,原因目前還不清楚(或許是Applet是不允許使用者直接去存取硬碟的資料,而是要直接透過網路的方式下載)。但是如果不進行簽章的動作,連透過http的方式去使用資源,也是會出現access denied的錯誤。

在使用ImageIcon的方式使用圖片,一般會像以下例子去取用資源
ImageIcon img = new ImageIcon("/picture/1.png");
這樣的情況在Applet下,是會出現access denied的錯誤,所以要改成下列的方式去取用資源
URL url = new URL("http://localhost/1.png");
ImageIcon img = new ImageIcon(url);
相關資料參考
JDK Tools and Utilities
JavaWorld

2007年11月5日 星期一

簡轉繁的利器 for Firefox

最近蠻常在逛大陸簡體的網站,發現還是不習慣看簡體字,以前在使用IE時,都有方便轉換的工具,那在Firefox上呢?

在網路上搜尋了一陣子,發現"新同文堂"這套針對Firefox用戶開發的"簡→繁"或"繁→簡"的工具。使用者可以直接在瀏覽的網頁上,使用"右鍵→新同文堂→簡轉繁"就可以將正在瀏覽的簡體網頁轉換成繁體,覺得不錯用,推薦給有使用Firefox的人嘍!

安裝方式:
‧下載"新同文堂"
‧透過Firefox選單中,"檔案→開啟檔案"的方式執行下載的xpi檔。
‧安裝後,重開Firefox即可使用。