2011年2月13日 星期日

在 Netbean+GlassFish 下開發 Sessiong Bean 及其 client 程式

本文件介紹如何使用 Netbeans 及 Glassfish 開發 Session Bean 及其 client 程式。內容包括:
  1.  建立並佈署 Session Bean (EJB 3.1)
  2.  在 Servlet 中使用 Glassfish 上的 Session Bean
  3.   在 JSP 中使用 Glassfish 上的 Session Bean
  4.  在 Stand-along application 中使用 Glassfish 上的 Session Bean
PDF 檔下載

2011年2月7日 星期一

檢查 browser 的 cookie 功能是否啟動

參考我的 wiki 網頁: 檢查 browser 的 cookie 功能是否啟動

網頁自動更新使用 meta refresh 指令

要讓 browser 能自動更新, 或者自動轉到另一個網頁使用指令:

<meta http-equiv="refresh" content="5" />

為什麼要用 meta refresh 而不用 HttpServletRequest.sendRedirect() 方法呢? 因為 sendRedirect() 方法在重新導向時, 無法將 cookie 傳回伺服器端.

Reference:

* Meta refresh
* p174, 精通JAVA WEB程式設計, 葉雅美 - 維科出版社 ,2004-10-01 出版

Swing 的背景程式

要在 Swing 中執行背景程式, 要使用 SwingWorker 物件.

繼承 SwingWorker 物件後, 要覆寫 doInBackground(). 在 doInBackground() 中放的就是我們要執行的背景程式內容.

將字串編碼成 html 格式

在寫 servlets/JSPs 時, 常要要輸出符合 html 格式的字串, 例如:

"bread" & "butter"  要變成 "bread" & "butter"


有現成的套件可以做此轉換.

org.apache.commons.lang.StringEscapeUtils 這個類別提供了:

public static String escapeHtml(String str)

可以將字串轉成 html 的格式.

Reference:
* Apache Commons
* escapeHtml()
* Robin's Tech Tips

2011年1月30日 星期日

Swing GUI 下背景工作程式

介紹在 Swing 程式下 javax.swing.SwingWorker。當在 Swing GUI 程式中要執行長時間的工作時, 可以利用 SwingWorker 物件, 把工作放到背景執行。 另外介紹在 Application framework (JSR 296) 下的 TaskService 及 TaskMonitor.

文件下載

2011年1月28日 星期五

Swing layout

Swing layout 會自動的調整元件的大小, 不同的 layout 調整大小時所遵照的規則有所不同, 整理如下:


BoxLayout:     requested maximum sizes
FlowLayout:      preferred sizes
BorderLayout:     preferred sizes
GridLayout:    Each component takes all the available space within its cell, and each cell is exactly the same size
GridBagLayout:   preferred sizes

2011年1月20日 星期四

Icon 介面中的 PaintIcon() 方法

Icon 介面中有一個抽象方法 PaintIcon(), 用來畫出 icon. 這個方法的宣告如下:


void paintIcon(Component c, 
               Graphics g,
               int x,
               int y)
方法裡有四個參數:
x, y: 畫 icon 的位置
g: 繪圖物件
c: 用來取得繪圖所需要的屬性, 如前景或背景顏色.  

2011年1月16日 星期日

Swing JTable 的使用

使用 Swing JTable 的建議驟:

1.繼承 AbstractTableModel
2. 實作以下的方法:
  public int getRowCount();
  public int getColumnCount();
  public Object getValueAt(int row, int column);
  
   getValueAt() 的方法主要傳回儲存資料的集合中的值. 
3. 若要改變 table 的 header, 要覆寫以下方法:
  
 String getColumnName(int column)
 
4. 撰寫一方法, 設定或更新儲存資料的集合. 更新完後, 記得到執行 void fireTableDataChanged()
或者其它 void fireXXX 的方法, 將資料顯示出來. 

  

2011年1月12日 星期三

有爭議的答案: How a deployment manager participate in a flexible management evnrionment?

IBM 000-377 模擬考題中, 有一個問題給的答案有爭議. 問題如下?

In Order for a deployment manager to participate in a flexible management environment, which one uf the following steps must be completed?

A. Register directly with the job manager.
B. Register with the node agent on the jobmanagers host.
C. Register with an administrative agent on the jobmanagers host
D. First register with a local administrative agent, and then register with the job manager.

模擬考題給的答案是 D.

但是, 和  page 45 in WebSphere Application Server V7 Administration and Configuration Guide 說得不太一樣. 在 Guide 中說 "If a deployment manager wants to participate in an environment controlled by a job manager, the deployment manager registers directly with the job manager; no administrative agent is involved in this case."

就再討論啦..

2011年1月11日 星期二

Shared library and isolated shared library

在 000-377 的考題中有兩題考 shared library 的題目. 有一題的答案是 share library 要 associate 到 application class loader. 另一個題的答案是 isoloated shared library 要 associate 到 web module class loader. 這是為什麼呢?

以下為這兩題的題目:

第一題:
A developer informs an administrator that a servlet in one of the Web modules of a new enterprise application needs to access code in a native library. The administrator creates a shared library and adds the native library to its Native library path. With which class loader should the shared library be associated so that the servlet can access the native library without risk of an UnsatisfiedLinkError?

A. The Web modules class loader
B. The nativelibrays class loader
C. The application servers class loader
D. The enterprise applications class loader

此題答案給的為 C.

另一題
A developer informs an administrator that a servlet in one of the Web modules of a new enterprise application needs to access code in a native library. The administrator creates a shared library and adds the native library to its Native library path. What else does the administrator need to do to ensure that the servlet can access the native library without risk of an UnsatisfiedLinkError? Using the administrative console, select:

A. Use an isolated class loader for this shared library on the shared library settings page and associate the shared library with the Web modules class loader.
B. Classes loaded with parent class loader first on the settings page for the enterprise applications class loader and associate the shared library with the enterprise applications class loader.
C. Use an isolated class loader for this shared library on the shared library settings page and inform the developer of the name of the shared library so the developer can include it in the appropriate Java code.
D. Classes loaded with local class loader first on the settings page for the Web modules class loader and associate the shared library with the Web modules class loader.

此題答案給的為 A.

2011年1月4日 星期二

建立 Swing 應用程式: Netbean vs Eclipse

在 NetBeans IDE 建立 Swing 應用程式的方式有數種. 一種就是用 NetBeans 所提供 的 Application Framework (JSR 296). 另外一種方式是使用 NetBeans Platform.

在 Eclipse 上建立 Swing 應用程式, 可使用 WindowBuilder Pro. 兩者都可以用拖曳的方式來產生 swing GUI.

這兩天試用下來的結果, 覺得 Netbeans 的 Application Framework 用得比較順手. NetBeans Platform 就改天再試了. WindowBuilder Pro 在製作 GUI 時, 有時會發生 Exceptions, 不知道是什麼原因.

2011年1月3日 星期一

Limitations on inner classes in methods

在某個方法中宣告 inner class, 例如

public void perform(){
  int localVar;
  MyInnerClass {
  }
}

使用這種型式的 inner class 有一個很重要的限制:
"any of the method's local variables that are referenced by the inner class must be declared final."

若有一個物件 myInnerClass 的方法中參考到 localVar 變數, 則此變數應宣告為 final (常數).

原因: 在 perform() 方法中所建立來的 MyInnerClass 物件所看到的 localVar 應是一樣的. 但每一個 MyInnerClass 物件又都是獨立的有自己的看到的 localVar. 所以這個 localVar 會被 copy 給每個 MyInnerClass 同時限制成唯讀.

Ref: Section 6.6.2 Inner Classes within methods in Learning Java, 2nd edition, O'Relly, 2002,

Inner class 用於處理 swing action 的處理

某個 swing 元件的 action listener 通常會使用 inner class 的技巧來處理. inner class 代表著一種 "live in" 的概念, 若 B 類別相依在 A類別, 沒有 A 就沒有 B 類別, 則 B 類別可以成為 A 類別的 inner class.

Inner class 的特性.

an inner class lets us avoid the encapsulation problem because it can access all the members of its enclosing instance.