Home

2009年5月15日 星期五

[PHP] AppServ 安裝 pear 及與 MySQL 連結設定

From: Polin Wei

環境:
OS: WindowXP Sp3
AppServ Package: appserv-win32-2.5.10.exe 包含的服務如下:
Apache 2.2.8
PHP 5.2.6
MySQL 5.0.51b
phpMyAdmin-2.10.3


Step 01:
appservnetwork: 下載appserv-win32-2.5.10.exe 並依預設目錄 C:\AppServ 安裝 Apache, PHP, MySQL, phpMyAdmin

Step 02:
修改 c:\windows\php.ini 檔. DLL (動態連結檔) 先載入 php_mbstring.dll, 再載入 php_exif.dll 順序如下:

; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.

; pear 要用的
extension=php_mbstring.dll
extension=php_exif.dll


Step 03: 安裝 PEAR
a. 重新啟動 Apache , 然後執行 C:\AppServ\php5\go-pear.bat,往後皆按Enter便可。
b. 最後會出現一段訊息,提醒使用者雙擊 c:\AppServ\php5\PEAR_ENV.reg ,登錄PEAR相關的註冊碼。

Step 04: 安裝 PEAR DB, 並使用"Pear list" 檢查是否安裝完成
C:\AppServ\php5>pear install DB
C:\AppServ\php5>pear list




Step 05 : 在 DB: test , 建立 Table: users 來測試
--
-- 資料庫: `test`
--
--------------------------------------------------------
--
-- 資料表格式: `users`
--

CREATE TABLE `users` (
`id` varchar(10) NOT NULL,
`username` varchar(30) NOT NULL,
`password` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- 列出以下資料庫的數據: `users`
--

INSERT INTO `users` (`id`, `username`, `password`) VALUES ('1', 'polin', 'wei');


Step 06: 建立 pearDB.php 來測試

<?php


require_once("DB.php");


$dbh = DB::connect('mysql://root:mysql@localhost/test');
$result = $dbh->query('SELECT * FROM users ORDER BY id');
if ( $result->numRows($result) > 0 ) {
 echo "<table>\n<tr><th>ID</th><th>userName</th><th>Password</th></tr>\n";
  while ( $data = $result->fetchRow() ) {
  echo "<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>\n";
  }
  echo "</table>\n";
} else {
  echo "Ok;\n";
}


?>


 




成果如下:
ID userName Password
1 polin wei

2009年5月10日 星期日

利用 Lambda Probe 來觀察 JNDI 的 DataSource Connection Pools

From: Polin Wei

在寫 Java 程式時, 利用 JNDI 來連接資料庫, 是一個非常好的方法, JNDI 的設定, 請參考: [JNDI] Java + Tomcat + MySQL 的連結設定 但是新手程式設計師常常建立了 DataSource 的 Connection , 在取完資料庫後, 卻又常常忘了關閉這一條 connection . 導致資料庫連接池 ( DataSource Connection Pools ) 不夠使用, 而整個 Application Server 當住了.

這有一個不錯的工具 Lambda Probe , 可以讓程式設計師來觀察程式撰寫的過程中, 是否有 Connection 忘了關閉的情況. 而 Lambda Probe 目前有支援 Tomcat & JBoss.

環境:
Tomcat : 6.0.14
Lambda Probe: LambdaProbe 1.7b, BINARIES (解開的檔案放在 D:\probe)

Step 01:
Lambda Probe 下載 LambdaProbe 1.7b, BINARIES , 並解開放在 D:\probe

Step 02:
修改 Tomcat 的 server.xml , tomcat-users.xml 分別如下:


server.xml
<Context docBase="D:/probe" path="/probe" privileged="true"/>

tomcat-users.xml
<role rolename="admin"/>
<user username="admin" password="1234" roles="admin,manager"/>


Step 03:
重啟 Tomcat

Step 04:
進入: http://localhost:8080/probe
輸入admin,1234



Step 05:
點選 Data Sources 的頁次, 就可以查看 DataSource Connection Pools 的狀況



參數說明:
在 Data Sources 頁次中的 M, E , B 分別為
M: Total max. 連接池最大的連接數目
E: Total estab. 現行已建立過的連接(connections) 數目
B: Total busy 目前的連接數

2009年5月7日 星期四

[FCKeditor] 用 Java Taglib 使用 FCKeditor

From: Polin Wei

FCKeditor 是所見即所得(WYSIWYG)的線上編輯器, 支援多國語言, 多種瀏覽器支援, 可更換SKIN,還可以用JavaScript控制按鈕,功能十分多樣化,外掛的功能也是十分強大,支援瀏覽器管理檔案...等多樣化外掛。

支援語言: ASP.Net, ASP, ColdFusion, PHP, Java, Active-FoxPro, Lasso, Perl, Python
支援流覽器: IE, FireFox, Safari, Opera, Google Chrome, Camino
支援平台: MS Windows , MAC , Linux
版權: under the GPL, LGPL and MPL open source licenses

在 Java 的平台上有一個很好用的工具 Taglib , 可以讓你的 jsp 程式簡單化.配置如下

FCKeditor: FCKeditor_2.6.4.zip
FCKeditor.Java: fckeditor-java-2.4.1-bin.zip
FCKeditor.Java 文件參考: http://java.fckeditor.net/
SLF4j: slf4j-1.5.6.zip
Application Server: Tomcat 6.x.x

step01:
在 WebContent 目錄下建立一個目錄 fckeditor , 並將 FCKeditor_2.6.4.zip 解開的資料放在這個目錄下.

step02:
解開 fckeditor-java-2.4.1-bin.zip , 將 fckeditor-java-core-2.4.1.jar, 及 lib 目錄下的三個檔案 commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar, slf4j-api-1.5.2.jar , 複製到 WEB-INF\lib

step03:
解開 slf4j-1.5.6.zip , 將slf4j-api-1.5.6.jar, slf4j-simple-1.5.6.jar 複製到 WEB-INF\lib

step04:
在 WEB-INF\web.xml 檔增加下列程式碼
<servlet>
 <servlet-name>Connector</servlet-name>
 <servlet-class>net.fckeditor.connector.ConnectorServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Connector</servlet-name>
  <url-pattern> /fckeditor/editor/filemanager/connectors/*
  </url-pattern>
</servlet-mapping>


step05:
在 WEB-INF 目錄下增加一個檔案 FCKeditor.tld , 內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 version="2.0">
 <description>
  The FCKeditor Tag Library offers a very convenient way to create
  several FCKeditor instances with different configurations.
  Additionally, you can check for user-based capabilities.
 </description>
 <display-name>FCKeditor Tag Library</display-name>
 <tlib-version>2.4</tlib-version>
 <short-name>FCK</short-name>
 <uri>http://java.fckeditor.net</uri>
 <tag>
  <description>
   Creates a FCKeditor instance with the given parameters. Any
   parameter except instanceName which is empty or contains
   whitespaces only will be ignored.
  </description>
  <display-name>editor</display-name>
  <name>editor</name>
  <tag-class>net.fckeditor.tags.EditorTag</tag-class>
  <body-content>JSP</body-content>
  <attribute>
   <description>
    The unique instance name under which the editor can be
    retrieved through the API.
   </description>
   <name>instanceName</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>
    Width of the FCKeditor instance in the browser window.
   </description>
   <name>width</name>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>
    Height of the FCKeditor instance in the browser window.
   </description>
   <name>height</name>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>
    The toolbar set which shall be displayed to the user.
   </description>
   <name>toolbarSet</name>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>
    The path/folder in which the editor is deployed under
    the given context. The context path will be attached
    automatically. (e.g. '/fckeditor')
   </description>
   <name>basePath</name>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>
    Passes any content to the FCKeditor document. Use the
    jsp:attribute tag for large inline content. \r, \n, and
    \t will be truncated.
   </description>
   <name>value</name>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <example><![CDATA[
<FCK:editor instanceName="editorDefault" height="500px" />]]>
  </example>
 </tag>
 <tag>
  <description>
   Sets a config property of the editor to the supplied value.
   You may provide any attribute you want for the editor. Set
   at least one attribute per tag or several attributes with
   one tag. This tag can only be nested within an editor tag.
   For all configuration options click
   <![CDATA[<a href="http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options">here</a>]]>.
  </description>
  <display-name>config</display-name>
  <name>config</name>
  <tag-class>net.fckeditor.tags.ConfigTag</tag-class>
  <body-content>empty</body-content>
  <dynamic-attributes>true</dynamic-attributes>
  <example>
   <![CDATA[
<FCK:config SkinPath="/skins/silver/" AutoDetectLanguage="true" />]]>
  </example>
 </tag>
 <tag>
  <description>
   Displays session-dependent and compatibility-related
   information. This tag is intended for developers only.
   Response messages cannot be localized, they are English
   only.
  </description>
  <display-name>check</display-name>
  <name>check</name>
  <tag-class>net.fckeditor.tags.CheckTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
   <description>
    Provide the feature name you want to check. Valid
    features are [FileUpload, FileBrowsing,
    CompatibleBrowser]
   </description>
   <name>command</name>
   <required>true</required>
   <type>java.lang.String</type>
  </attribute>
  <example><![CDATA[
<FCK:check command="FileUpload" />
<FCK:check command="CompatibleBrowser" />]]>
  </example>
 </tag>
</taglib>


step06:
建立一個 fck.jsp 來測試, 程式碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/FCKeditor.tld" prefix="FCK"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="show.jsp" method="post" target="_blank">
<FCK:editor instanceName="fckEdit"  width="700" height="500"  toolbarSet="Default" />
<input type="submit" value="Submit">
</form>


</body>
</html>



成果如下圖:


fckeditor-java-2.4.1 的設定比較簡便, 就連上載檔案, 也只要在step04: 設定好 servlet : Connector 就可以了. 不需要太繁瑣. 不過若要上載檔案, 不要忘了要在 classpath 建立一個檔案 fckeditor.properties , 內容如下:
fckeditor.toolbarSet=Basic
connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl
connector.userFilesPath=/userfiles
connector.resourceType.file.extensions.allowed=txt|doc
connector.resourceType.image.extensions.allowed=jpg|png


這樣才不會有問題. 參數說明:
connector.userFilesPath : 是伺服器上要存放檔案的地方.如 /userfiles
connector.resourceType.file.extensions.allowed : 可以限制上傳到/userfiles/file 目錄的附加檔名有那些
connector.resourceType.image.extensions.allowed: 可以限制上傳到/userfiles/image 目錄的附加檔名有那些

因為上載的檔案只能存在這個 application WEBcontent 的目錄內, 所以若將 java 打包成 war 檔在 JBoss 上使用, 則會有問題.