วันพฤหัสบดีที่ 1 กันยายน พ.ศ. 2554

การใช้ภาษาไทย กับ java web application (servlet, jsp)

การใช้ภาษาไทย กับ java web application (servlet, jsp)
การใช้งาน java web app กับภาษาไทย มักจะมีปัญหา เรื่องไม่แสดงผลตามที่ต้องการ หรือใส่ข้อมูลหน้าเว็บ เป็นภาษาไทย แต่เมื่อ submit แล้ว กลับแสดงผลออกมาเป็นตัวอะไรก็ไม่รู้ ดังนั้นผมจึงได้รวบรวมข้อมูล และเสนอวิธีการจัดการเกี่ยวกับ การแสดงผลภาษาไทย และภาษาอื่น ๆ ของ application ที่ทำงานบน java web app container ดังนี้


รู้จัก Character set Encoding (ขอเรียกสั้น ๆ ว่า charset) ที่ใช้กับภาษาไทย
เนื่องจากปัจจุบันการเก็บข้อมูลของ character นั้นเป็นแบบ multibyte character หรือก็คือ 1 ตัวอักษร ใช้มากกว่า 1 byte ในการเก็บข้อมูล ซึ่งไม่เหมือนในสมัยก่อนที่ character 1 ตัวอักษรมีขนาดข้อมูลเท่ากับ 1 byte แต่การเก็บข้อมูลแบบ 1 character ต่อ 1 byte ก็ยังคงใช้อยู่ ดังนั้น ใน OS ที่รองรับการแสดงผลหลายภาษา จะมีการเข้ารหัสข้อมูลเพื่อให้รหัสตัวอักษรแต่ละตัว ตรงกับ ตารางตัวอักษรมาตรฐาน ของแต่ละภาษาก่อน ซึ่ง charset ที่ใช้กับภาษาไทยได้ มีดังนี้

TIS-620
WINDOWS-874


ส่วน charset ที่สามารถรองรับได้หลายภาษาพร้อมกันนั้น เรามักจะใช้ UTF-8 นั้นหมายความว่า ถ้าในเพจ หน้าหนึ่งของเรา ต้องแสดงผลหลาย ๆ ภาษา ควรจะต้องใช้ charset UTF-8

ในการเขียนโปรแกรมภาษาจาวา โครงสร้างข้อมูลพื้นฐานที่เก็บตัวอักษร (char) จะเก็บเป็น unicode ซึ่งจะกินเนื้อที่ 2 ไบต์ ข้อมูลตัวอักษรใด ๆ ที่ใช้ char หรือ class String อ้างอิงเพื่อประมวลผล จะถูกแปลงให้อยู่ในโีครงสร้างข้อมูลดังกล่าวก่อน ดังนั้น และใน class String จะมี method byte[] getBytes(charset) ซึ่งใช้สำหรับ encoding string ให้อยู่ในลำดับของ byte ด้วย charset ที่กำหนด ซึ่ง method นี้จะมีประโยชน์มาก เมื่อเราต้องการเก็บข้อมูลตัวอักษร ลงฐานข้อมูลที่ใช้ charset ต่างกัน หรือแปลงข้อมูลอักษรลงไฟล์ เพื่อไปใช้ในระบบอื่นที่ต้องการ charset ต่างออกไป

String s = new String(byte[],charsetDecoder)

โดย charset decoding คือ ชื่อของ decoding ที่ต้องการถอดรหัส ซึ่งควรจะเป็นชื่อเดียวกับ charset ที่ encoding ไว้ มิฉะนั้นอาจจะได้ String ที่อ่านไม่ออก

เมื่อเราพอจะรู้เรื่องของ encoding บ้างแล้ว ก็มาเริ่มทำขั้นตอนต่อไปเลยดีกว่า ซึ่งขั้นตอนต่อไปนั้น ทำให้เราสามารถประหยัดเวลาในการเขียนโปรแกรมขึ้นมาก

การใช้ html meta tag
เมื่อเราเขียน servlet หรือ jsp มาถึงขึ้นตอนการเขียนโปรแกรมแสดงผล ในบรรทัดแรกเลยที่จะใช้คำสั่ง out.println หรือถ้าใน jsp ก็หมายถึงบรรทัดแรก ๆ เราควรจะให้ output meta tag ก่อน เพราะใน meta tag มี attribute ที่ให้เราสามารถกำหนด character set encoding ได้ ดังนี้

servlet
out.println("");

JSP

และควรกำหนดที่ page directive tag ของ jsp ด้วยดังนี้
<%@ page contentType="text/html;charset=UTF-8"%>

ผลของการกำหนด meta tag จะทำให้ browser ที่แสดงผลปลายทาง ทำการ decoding ข้อมูลที่ได้รับจาก web server ตาม charset ที่กำหนด และ ทำการ encoding ข้อมูลที่ได้รับจาก form ในหน้าเว็บ ส่งให้ web server

ดังนั้น ถ้าเราไม่กำหนด meta tag ในส่วนนี้ browser ก็จะใช้ default charset ของ browser แต่ละยี่ห้อทำการ encoding และ decoding ( ซึ่งจะทำให้เราไม่สามารถควบคุมได้ เพราะเราก็รับประกันไม่ได้ว่าผู้ใช้จะใช้ browser ยี่ห้อใดบ้าง หรือ ผู้ใช้ตั้งค่า charset ของ browser ไว้เองแล้วหรือไม่

หมายเหตุ ส่วนใหญ่ browser จะใช้ default charset เป็น ISO-8859-1

การตั้งค่า compile Encoding
คือการกำหนดค่าให้กับ java compiler และ jsp compiler ว่า source code ที่จะ compile ต้องการใช้ encoding อะไร ซึ่งการกำหนด compile Encodeing จะมีผลโดยตรงกับ String ที่เราใช้แสดงผลที่ hard code ไว้ใน source code ถ้าเรากำหนด compile Encoding ที่ไม่รองรับภาษาที่เราใช้ ผลที่แสดงออกมาอาจจะผิดพลาดได้ เราสามารถ ตั้งค่า compile Encoding ได้ดังนี้

Servlet
ถ้าเราใช้ IDE ก็ให้ตั้งค่า Text file encoding ใน IDE นั้น ๆ ได้เลย แต่ถ้าเราใช้ command line java compile (javac) ให้ใส่ option เข้าไปดังนี้

javac -encoding TIS-620 myservlet.java

JSP
กำหนดได้ที่ attribute pageEncoding ของ Page directive เพื่อให้ JSP compile รู้ได้ดังนี้

<%@ page pageEncoding="TIS-620" %>

การตั้งค่า Encoding ให้กับ class HttpServletRequest และ HttpServletResponse
จากที่กล่าวมาแล้วในข้อที่ 2 ดังนั้น servlet และ jsp ที่มีการนำข้อมูลที่ส่งผ่าน form จากหน้าเว็บมาประมวลผล จะต้อง set charecterEncoding ให้กับ instance ของ HttpServletRequest (ใน jsp ก็คือ request) โดยเราจะต้อง set charset ให้ตรงกับที่ตั้งไว้ใน html meta tag หรือ page directive contentType ซึ่งถ้าเราไม่ได้ set charset ให้ ค่า charset ปกติจะเป็น ISO-8859-1 ซึ่งเมื่อ charset ไม่ตรงกัน string ที่ได้ก็จะผิด ส่วน instance ของ HttpServletResponse (ใน jsp ก็คือ response) นั้น จะถูกตั้งค่าไว้ตาม page directive contentType หรือตาม charset ที่ webcontainer ตรวจสอบได้ หรือเราตั้งค่าเองไว้ใน code

ตัวอย่างการใช้งาน
request.setCharacterEncoding("UTF-8");
String st = request.getParameter("param1");

response.setCharacterEncoding("TIS-620");

เพื่อความสะดวกมากขึ้น ผู้ออกแบบโีครงสร้าง web app ของจาวาได้ออกแบบ servlet filter class ซึ่ง servlet filter จะช่วยให้เราปรับแต่งค่าของ HttpServletRequest และ HttpServletResponse ซึ่งเราสามารถใช้ servlet filter ตั้งค่า charset สำหรับ jsp และ servlet ทุก ๆ หน้าได้ ทำให้ไม่ต้องโค้ดคำสั่ง request.setCharacterEncoding บ่อย ๆ ซึ่งวิธีการสร้าง class และ ใช้งานทำได้ดังนี้

4.1 สร้าง class servlet filter โดย มีตัวอย่างดังนี้


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class UTF8Filter implements Filter {

FilterConfig filterConfig;

public UTF8Filter(){
super();
}

public void init(FilterConfig arg0)
throws ServletException {
filterConfig = arg0;

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

if (chain != null)
chain.doFilter(request, response);
}

public void destroy() {
filterConfig = null;
}

}


จากโค้ดข้างบนนี้ เป็น filter ที่ตั้งค่า charset ให้กับ ServletRequest กับ ServletResponse เป็น UTF-8

4.2 เมื่อ คีย์โค้ดข้างบนแล้ว เราก็ต้องเขียน descriptor code เพิ่มไปใน web.xml โดย วางตำแหน่งไว้ใน tag ดังนี้


<filter
>
<filter-name>UTF8Filter</filter-name>
<filter-class>web.filter.UTF8Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>UTF8Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

หากเราต้องการให้ filter มีผลกับเฉพาะบางไฟล์ ก็ให้เราไปแก้ไข descriptor ที่ tag url-pattern

ข้อมูลอ้างอิงจาก :

http://www.narisa.com/forums/index.php?showtopic=1800
http://java.sun.com/products/jsp/syntax/1.2/syntaxref12.html
และประสบการณ์ที่ผ่านมา
อ้างอิงจาก blogger:

http://buewhite-javatip.blogspot.com/2007/12/java-web-application-servlet-jsp.html

วันพฤหัสบดีที่ 25 สิงหาคม พ.ศ. 2554

การใช้งาน JNDI Datasource ของ Tomcat&MySQL

อย่าลืมนำไฟล์ mysql-connector-java-5.0.7-bin.jar ไปวางไว้ที่ path web server เช่น

D:\LHDEV\Server\apache-tomcat-6.0.18\lib

เพื่อให้ web server รู้จัก และใช้งาน JNDI datasource ได้ครับ


วันอาทิตย์ที่ 12 มิถุนายน พ.ศ. 2554

การใช้งาน command MySQL 5.0

บางครั้งการใช้งาน database ในเครื่อง production ไม่มี GUI สำหรับ manage database server มีความจำเป็นต้องจำ command สำหรับใช้งานผ่าน หน้าจอ dos

cd C:\Program Files\MySQL\MySQL Server 5.0\bin
#mysql --user=root --password=root
or #-u root -p
or #-u root -pyourpassword
#show databases;
#use db_xxx;
#show tables;
#select,update,delete
#Exit;
แสดง field ในตาราง :
#show columns from table;
#status
#แสดงรายชื่อผู้ใช้ที่สามารถเข้า MySQL
mysql> use MySQL;

วันพฤหัสบดีที่ 5 พฤษภาคม พ.ศ. 2554

Exception :Cannot find the specified class java.security.PrivilegedActionException

ใคร,ทำอะไร,ที่ไหน,ทำอย่างไร,ทำแบบไหน ,ทำเพื่ออะไร,ปัญหาคือ ?

สิ่งที่ต้องการ หรือ output ?

Solution ที่ใช้ในการแก้ปัญหา ?

Key word : Cannot find the specified class java.security.PrivilegedActionException

Environment & tools

Environment & tools :

#IBM Rational Application Developer 7.0

#WebSphere Application Server 6.1

#JDK1.5

#Windows XP

#os.arch=x86

#os.name=Windows XP

#os.version=5.1 build 2600 Service Pack 3

#java.fullversion=J2RE 1.5.0 IBM J9 2.3 Windows XP x86-32 j9vmwi3223-20060504 (JIT enabled)

#java.vendor=IBM Corporation

#: java.version=1.5.0

#: java.vm.info=J2RE 1.5.0 IBM J9 2.3 Windows XP x86-32 j9vmwi3223-20060504 (JIT enabled)

J9VM - 20060501_06428_lHdSMR

JIT - 20060428_1800_r8

GC - 20060501_AA

Site HTTPS: https://203.146.18.xx/PGPayment/directpgw.aspx

Certificate ของ Site https://203.146.18.xx/PGPayment/directpgw.aspx

Save ไว้ที่ "D:\certs\cert_tbank_test.cer"

ปัญหาคือ : ไม่สามารถ Connection Site HTTPS ได้ จะมี Errors ดังนี้

Exception err:java.net.SocketException: Cannot find the specified class java.security.PrivilegedActionException: java.lang.ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory

หรือ Not trust certificate on jks

สิ่งที่ต้องการ หรือ output คือ :ต้องการทำ TestCase สำหรับ Connection Site ที่เป็น HTTPS โดยใช้รูปแบบที่เป็น HTTP POST แบบ Server to Server

ให้ run Method สำหรับ connection site https://203.146.18.xx/PGPayment/directpgw.aspx เพื่อต้องการค่าบางจาก เช่น ค่าที่มีการ Approve จาก bank หรือ Errors ต่างๆ

Solution ทีใช้ในการแก้ปัญหาคือ มีขั้นตอนดังนี้:

1 , source code HTTP POST method sender to site สำหรับ Test connection

ใช้ HttpClient POST

private static String sender2TBankTest(String paramInfo) throws IOException{

String ssl_filepath = "D:\\DEV\\IBM\\SDP70\\runtimes\\base_v61\\profiles\\AppSrv01\\etc\\DummyClientTrustFile.jks";

String ssl_password = “WebAS”

String K_PAYMENT_URL ="https://203.146.18.76/PGPayment/directpgw.aspx";

PostMethod post = new PostMethod(K_PAYMENT_URL);

System.out.println("##---->[sender2TBankTest] url_https : "+K_PAYMENT_URL);

try {

System.out.println("##---->[sender2TBankTest] paramInfo :"+paramInfo);

java.util.Properties props= new java.util.Properties(System.getProperties());

props.put("javax.net.ssl.trustStore",ssl_filepath);

props.put("javax.net.ssl.trustStorePassword",ssl_password);

props.put("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

java.util.Properties newprops = new java.util.Properties(props);

System.setProperties(newprops);

System.out.println("##----->[sender2TBankTest] System.setProperties() OK...");

HttpClient httpclient = new HttpClient();

HttpClientParams param = new HttpClientParams();

param.setAuthenticationPreemptive(true);

System.out.println("##---->[sender2TBankTest] set Authentication Preemptive OK....");

//encrypt

String pgwMsgEnc = SinaptIQ3DES.encrypt(paramInfo);

System.out.println("##----->[sender2TBankTest] Encrypt:"+pgwMsgEnc);

//add parameter ถ้าต้องการ

post.addParameter(TBankConstant.PGWMSG, pgwMsgEnc);

String result=null;

String contenners=null;

//execute

int statusCode = httpclient.executeMethod(post);

System.out.println("##---->[sender2TBankTest] Status = " + statusCode);

if(statusCode != -1 ) {

contenners = post.getResponseBodyAsString();

//logger.info("response from post method :==> " + contents);

if(contenners!=null){

result = contenners;

}

else{

result = paramInfo;

}

System.out.println("##----->[sender2TBankTest] Connection 2 Tbank Ok.....");

}

System.out.println("##----->[sender2TBankTest] Result length:"+contenners.length());

System.out.println("##----->[sender2TBankTest] Result :"+contenners);

return result;

}

finally{

post.releaseConnection();

}

}

2, ให้ทำการแก้ไข “java.security” %PATH%/java/jre/lib/security/java.security

ตัวอย่างเช่น

D:\DEV\IBM\SDP70\runtimes\base_v61\java\jre\lib\security\java.security

มองหา Tag ดังนี้

กรณีที่ต้องการทำ TestCase

ssl.SocketFactory.provider=com.ibm.jsse2.SSLSocketFactoryImpl

ssl.ServerSocketFactory.provider=com.ibm.jsse2.SSLServerSocketFactoryImpl

ถ้าไม่มีให้ทำการเพิ่มเข้าไปตาม code ข้างบนเพื่อให้ JVM รู้จัก และนำ Class com.ibm.*…. มาใช้งานสำหรับทำ HTTP POST กับ Site ที่เป็น HTTPS ซึ่งจะเกี่ยวกับ Security

กรณ๊ที่ Run โดย Websphere Application Server ให้ใน tag default ดังนี้

ssl.SocketFactory.provider=com.ibm.websphere.ssl.protocol.SSLSocketFactory

ssl.ServerSocketFactory.provider=com.ibm.websphere.ssl.protocol.SSLServerSocketFactory

3,จากนั้นก็ให้ทำการ trust certificate ให้กับ WebSphere Application Server หรือ JVM หรือ KeyStore สำหรับ Connection Site HTTPS มี command ดังนี้

3.1 set JVM path สำหรับ key tool ดังนี้

C:\> set PATH= D:\DEV\IBM\SDP70\runtimes\base_v61\java\jre\bin;%PATH%

C:\> java –version //เป็นการตรวจสอบ version ของ jdk ว่าตรงกับที่ set path หรือไม่

Command สำหรับ Import certificate ให้กับ DummyClientTrustFile.jks เพื่อใช้ในการ connection site https

keytool -import -alias tbank_test_cert61 -trustcacerts -file "D:\certs\cert_tbank_test.cer" -keystore "D:\DEV\IBM\SDP70\runtimes\base_v61\profiles\AppSrv01\etc\DummyClientTrustFile.jks" -storepass WebAS

สำหรับ WAS6.1 มีการใช้ key รูปแบบใหม่ ที่เป็น .p12 ซึ่งสามารถ trust certificate ให้ ดังนี้

keytool -import -alias tbank_test_cert61 -trustcacerts -file "D:\certs\cert_tbank_test.cer" -storetype pkcs12 -keystore "D:\DEV\IBM\SDP70\runtimes\base_v61\profiles\AppSrv01\config\cells\AI-MAITREENode02Cell\nodes\AI-MAITREENode02\trust.p12" -storepass WebAS

ให้ trust certificate .p12 หรือ jks ก็ได้รองรับได้ทั้งสอง key จากนั้น จะมีคำถามให้ตอบ ว่าต้องการ Trust cert หรือไม่ ให้ ตอบ yes

ถ้าไม่มี Errors อะไรแสดงว่ามีการ trust certificate ให้กับ KEY แล้วเพื่อความมั่นใจเราสามารถแสดง รายการของ certificate ที่ trust ด้วย command ดังนี้

keytool -list -v -keystore "D:\DEV\IBM\SDP70\runtimes\base_v61\profiles\AppSrv01\etc\DummyClientTrustFile.jks" > "D:\checkCerts.txt" -storepass WebAS

ถ้ากรณีที่ trust certificate ให้กับ .p12 ก็ให้เปลี่ยนจาก DummyClientTrustFile.jks เป็น cert.p12

*******************************************

Alias name: tbank_test_cert61

Creation date: 5 พ.ค. 2554

Entry type: trustedCertEntry

Owner: CN=203.146.18.76, OU=Retail and SME E-Business Department, O="TESTBANK Public Co., Ltd.", L=Ratburana, ST=Bangkok, C=TH

Issuer: CN=SinaptIQ CA

Serial number: 6805c300000000000004

Valid from: 15/11/2553, 8:03 น. until: 15/11/2558, 8:13 น.

Certificate fingerprints:

MD5: 33:C5:27:3F:04:29:18:78:2C:BE:44:49:1F:9D:74:48

SHA1: 64:B0:A3:A6:5C:83:04:04:F6:02:9D:08:6A:B7:49:58:F5:73:50:B9

*******************************************

แสดงว่ามีการ trust เรียบร้อยแล้ว แค่นี้ก็สามารถ run test case เพื่อทดสอบ module ได้และไม่เกิด Exception ดังกล่าวแล้วครับ

ถ้ากรณีที่ run test case ผ่าน WebSphere Application Server ก็ให้มีการ แก้ไข java.security กับเป็นค่า default และ trust certificate ผ่าน Adminconsole หรือ ผ่าน command line ได้

วันอังคารที่ 3 พฤษภาคม พ.ศ. 2554

การ Set System.proterties ของ JVM

************การใช้งานและ List ******************
// add by pradoem 2010-10-23
String SSL_FILE_PATH = PropertyUtil.getProperty("PROXY_CONFIG_EURO","PROXY_SSL_PATH");
String SSL_PASSWORD = PropertyUtil.getProperty("PROXY_CONFIG_EURO","PROXY_SSL_PASSWORD");

java.util.Properties props= System.getProperties();

//java.util.Properties props= new java.util.Properties(System.getProperties());
props.put("javax.net.ssl.trustStore",SSL_FILE_PATH);
props.put("javax.net.ssl.trustStorePassword",SSL_PASSWORD);

//remark by pradoem
props.put("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
//remark by pradoem


java.util.Properties newprops = new java.util.Properties(props);
System.setProperties(newprops);



//System.setProperties(props);
PrintTest.printProproties();




**************Method show Properties System ของ JVM ****************
public static void printProproties()
{
Properties pr = System.getProperties();
TreeSet propKeys = new TreeSet(pr.keySet()); // TreeSet sorts keys

int i = 1;
for (Iterator it = propKeys.iterator(); it.hasNext(); ) {
String key = (String)it.next();
System.out.println(i+": "+key +"="+ pr.get(key));
i++;
}
System.out.println("test:"+pr.size());
}

วันพฤหัสบดีที่ 16 ธันวาคม พ.ศ. 2553

Was6.1 connection datasource db2 ไปยังเครื่อง database server ไม่ได้ เพราะว่าติด firewall น่ะครับ

วันพุธที่ 3 พฤศจิกายน พ.ศ. 2553

Selenium IDE simulate Test Tools ครับ

details :Selenium IDE is an integrated development environment for Selenium tests. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly...

reference :http://release.seleniumhq.org/selenium-ide/1.0.6/
https://addons.mozilla.org/en-US/firefox/addon/2079/

คำอธิบายเพิ่มเติม สำหรับจำลอง Test ที่ต้องการ volume เยอะๆ