2004-08-19
Layout Change
I am making some changes in the template just now. Also, I am going to start using the commenting facility of Blogger.com. There may some problems for a while.
# Posted at 16:31 by Ghasem (3 comments)
|
Ghasem Kiani's Weblog Notes about science, literature, society, and arts |
2004-08-19Layout Change I am making some changes in the template just now. Also, I am going to start using the commenting facility of Blogger.com. There may some problems for a while. # Posted at 16:31 by Ghasem (3 comments) Using Reflection for Actions There is no doubt that inner classes are an important aspect of the Java programming language. The easiest way to define an action in a GUI application is to subclass the If we have an action named The The source code for the
Here is a sample application using the
Since I am willing to see the result of a code when I read an example code in an article, I present here a few screenshots of the
This figure shows that the reflection has actually taken place, and the right method hase been called after clicking the Red and Green button.
Another screenshot, just to make it a little saltier! (I don't know if you can take the Persian nuances of words.)
Some may understandably argue that this is againtst the spirit of the Java programming language, having a strong object-oriented orientation. But, it is an equally important argument that such an attitude is the rule in languages like Delphi and lisp (from a totally different point of view relative to Delphi -- here I mean lexical closures and metaprogramming, if I use the right terms). The source and compiled classes of this example can be downloaded from here. # Posted at 03:24 by Ghasem (0 comments) 2004-08-06Encoding in JavaServer Pages Encoding is an important issue in software i18n. Iranian developers have spent enormous energy in tackling encoding problems, ever since computer programming entered to this country. Fortunately, Java has a marvellous support for various encodings, including Unicode and its variants. Regarding the encoding issue in JSP applications, these things should be taken in mind: Encoding of the JSP Page Itself The best solution for this problem is to write JSP documents, i.e., JSP pages that are standard XML documents. The XML standard has an efficient mechanism for determining the document encoding: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"> <!-- Page content... --></jsp:root> Encoding of the Response By setting the content type in the following manner, we can declare the encoding of the response that is sent to the client: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"> <jsp:directive.page contentType="text/html; charset=UTF-8" /> <!-- Page content... --></jsp:root> But there is a problem if you use the <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <!-- Page content... --></jsp:root> Encoding of the Request Setting the character encoding of the HTTP request is essential for proper interpretation of form data. This can most easily be done by using a JSTL tag: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <!-- Page content... --></jsp:root> Encoding of Database Connections If you are using pure java JDBC relational database management systems and drivers, such as IDB, HSQLDB, and PostgreSQL, you will not have any problems regarding the character encoding of the database connection. But if you are using the However, in recent versions of the JDBC-ODBC bridge, there is a way to set the connection charset: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <jsp:scriptlet><![CDATA[ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Properties p = new Properties(); p.put("charSet", "windows-1256"); p.put("user", "ghasem"); p.put("password", "100"); Connection c = DriverManager.getConnection("jdbc:odbc:MyTestDb", p); // ... ]]></jsp:scriptlet> <!-- Page content... --></jsp:root> Of course, it would be much better to use this DataSource instead: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:sql="http://java.sun.com/jsp/jstl/sql"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <jsp:useBean scope="page" id="ds" class="sun.jdbc.odbc.ee.DataSource" /> <c:set target="${ds}" property="databaseName" value="MyTestDb" /> <c:set target="${ds}" property="user" value="ghasem" /> <c:set target="${ds}" property="password" value="11" /> <c:set target="${ds}" property="charSet" value="windows-1256" /> <sql:setDataSource scope="page" var="source" dataSource="${ds}" /> <sql:query scope="page" var="res" dataSource="${source}"><![CDATA[ SELECT author, title, url FROM Books WHERE author like '%مطهري%' ]]></sql:query> <!-- Page content... --></jsp:root> Note: The following software have been used for creating and testing these code snippets (earlier versions may have some discrepancies):
Surely, much more can be said about JSP. These code fragments are only guides to show the ways we can set encoding in JSP applications. Each of the technologies mentioned above (servlets, JSP, JDBC, XML, JSTL, etc.) warrants a much deeper explanation. I may write some more notes about these technologies in the future. رمزگذاري در صفحات سرور جاوا رمزگذاري (encoding) يكي از مهمترين مسايل در بينالملليسازي برنامهها است, و در طول ساليان اخير, مقدار زيادي از تلاش برنامهنويسان ايراني را به خود مشغول كرده است. خوشبختانه, جاوا بهترين پشتيباني را براي ارائهي برنامهها با رمزگذاريهاي معتبر (مثلاً يونيكد) دارد. در مورد رمزگذاري صفحات سرور جاوا چند نكته حايز اهميت است: رمزگذاري خود صفحهي JSP بهتر است از سند JSP استفاده كنيم, يعني صفحاتي كه سند XML هستند. در اين صورت, رمزگذاري در ابتداي پرونده مشخص ميشود: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"> <!-- متن صفحه... --></jsp:root> رمزگذاري پاسخ براي تعيين رمزگذاري صفحهاي كه به دست مشتري ميرسد, نوع محتوا را تعيين ميكنيم: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"> <jsp:directive.page contentType="text/html; charset=UTF-8" /> <!-- متن صفحه... --></jsp:root> مسئله اين است كه اگر از برگهي (يعني tag) تعيين محل <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <!-- متن صفحه... --></jsp:root> رمزگذاري تقاضا تعيين اين رمزگذاري در گرفتن دادههاي فرم اهميت زيادي دارد. در اين مورد, بهترين كار استفاده از JSTL است: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <!-- متن صفحه... --></jsp:root> رمزگذاري پايگاه دادهاي در گردانههاي JDBC جاواي خالص كه خود پايگاه دادهاي تشكيل ميدهند (خود سرور پايگاه دادهاي هستند), مانند IDB و HSQLDB و PostgreSQL معمولاً مشكلي خاصي وجود ندارد. ولي وقتي از گردانهي با اين حال, در نسخههاي جديد پل JDBC و ODBC, امكان تعيين رمزگذاري ارتباط به صورت زير وجود دارد: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <jsp:scriptlet><![CDATA[ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Properties p = new Properties(); p.put("charSet", "windows-1256"); p.put("user", "ghasem"); p.put("password", "100"); Connection c = DriverManager.getConnection("jdbc:odbc:MyTestDb", p); // ... ]]></jsp:scriptlet> <!-- متن صفحه... --></jsp:root> البته, راه بهتر آن است كه از اين DataSource استفاده شود: <?xml version="1.0" encoding="windows-1256" ?> <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:sql="http://java.sun.com/jsp/jstl/sql"> <c:set target="${pageContext.response}" property="characterEncoding" value="UTF-8" /> <jsp:directive.page contentType="text/html" /> <fmt:setLocale value="ar-SA" /> <fmt:requestEncoding value="UTF-8" /> <jsp:useBean scope="page" id="ds" class="sun.jdbc.odbc.ee.DataSource" /> <c:set target="${ds}" property="databaseName" value="MyTestDb" /> <c:set target="${ds}" property="user" value="ghasem" /> <c:set target="${ds}" property="password" value="11" /> <c:set target="${ds}" property="charSet" value="windows-1256" /> <sql:setDataSource scope="page" var="source" dataSource="${ds}" /> <sql:query scope="page" var="res" dataSource="${source}"><![CDATA[ SELECT author, title, url FROM Books WHERE author like '%مطهري%' ]]></sql:query> <!-- متن صفحه... --></jsp:root> توجه: براي تهيهي اين متنهاي JSP و آزمايش آنها از نرمافزارهاي زير استفاده شده است (نسخههاي پايينتر ممكن است تفاوتهايي داشته باشند):
روشن است كه در بارهي صفحات سرور جاوا حرفهاي زيادي براي گفتن وجود دارد. متنهاي ارائه شده در اينجا صرفاً براي نشان دادن راهكارها و قابليتهاي تعيين رمزگذاري بود. در بارهي هر كدام از مباحث فوق (سرولت, JSP, كتابخانههاي برگه [مانند JSTL], ارتباط با پايگاههاي دادهاي [فناوري JDBC], و غيره) مطالب زيادي ميتوان گفت, كه شايد بعداً به برخي از آنها بپردازم. # Posted at 18:08 by Ghasem (0 comments) |
|
| Home | Archive |
| Copyright © 2003-2006, Ghasem Kiani |