lqxue

          常用鏈接

          統計

          book

          tools

          最新評論

          #

          java 1.5 捕獲線程異常

          Executor 提供了管理終止的方法,以及可為跟蹤一個或多個異步任務執行狀況而生成 Future 的方法。

          可以關閉 ExecutorService,這將導致其停止接受新任務。關閉后,執行程序將最后終止,這時沒有任務在執行,也沒有任務在等待執行,并且無法提交新任務。

          通過創建并返回一個可用于取消執行和/或等待完成的 Future,方法 submit 擴展了基本方法 Executor.execute(java.lang.Runnable)。方法 invokeAnyinvokeAll 是批量執行的最常用形式,它們執行任務集合,然后等待至少一個,或全部任務完成(可使用 ExecutorCompletionService 類來編寫這些方法的自定義變體)。

          Executors 類提供了用于此包中所提供的執行程序服務的工廠方法。

          用法示例

          下面給出了一個網絡服務的簡單結構,這里線程池中的線程作為傳入的請求。它使用了預先配置的 Executors.newFixedThreadPool(int) 工廠方法:
           class NetworkService {
          private final ServerSocket serverSocket;
          private final ExecutorService pool;

          public NetworkService(int port, int poolSize) throws IOException {
          serverSocket = new ServerSocket(port);
          pool = Executors.newFixedThreadPool(poolSize);
          }

          public void serve() {
          try {
          for (;;) {
          pool.execute(new Handler(serverSocket.accept()));
          }
          } catch (IOException ex) {
          pool.shutdown();
          }
          }
          }

          class Handler implements Runnable {
          private final Socket socket;
          Handler(Socket socket) { this.socket = socket; }
          public void run() {
          // read and service request
          }
          }

          posted @ 2008-11-05 15:46 lqx 閱讀(484) | 評論 (0)編輯 收藏

          js壓縮

          壓縮不僅僅可以提高用戶的下載速度,同時還可以加密代碼,下面說下一個常用的js壓縮方法:

          首先使用dojo的工具shrinksafe(http://shrinksafe.dojotoolkit.org/)壓縮一下,dojo的這個 工具會去掉注釋,他的壓縮不是簡單的替換變量,而是利用了mozilla的一個工具,對js解析后才壓縮,確保壓縮后的代碼不會出錯。

          dojo壓縮后,并不會減少太多,下一步可以使用http://javascriptcompressor.com/這個站點進行更高層次的壓縮,可惜只能登陸這個站點再壓縮,只能將你的js代碼復制的他的文本框,然后等他的壓縮輸出

          經過這2步,你的js會變得既安全,文件又小

          posted @ 2008-11-04 17:53 lqx 閱讀(191) | 評論 (0)編輯 收藏

          為何jsp 在resin下 亂碼,但在tomcat下卻工作良好?

          關于JSP頁面中的pageEncoding和contentType兩種屬性的區別:

          pageEncoding是jsp文件本身的編碼

          contentType的charset是指服務器發送給客戶端時的內容編碼

          JSP要經過兩次的“編碼”,第一階段會用pageEncoding,第二階段會用utf-8至utf-8,第三階段就是由Tomcat出來的網頁, 用的是contentType。Phontol.com

          第一階段是jsp編譯成.java,它會根據pageEncoding的設定讀取jsp,結果是由指定的編碼方案翻譯成統一的UTF-8 JAVA源碼(即.java),如果pageEncoding設定錯了,或沒有設定,出來的就是中文亂碼。Phontol.com

          第二階段是由JAVAC的JAVA源碼至java byteCode的編譯,不論JSP編寫時候用的是什么編碼方案,經過這個階段的結果全部是UTF-8的encoding的java源碼。Phontol.com

          JAVAC用UTF-8的encoding讀取java源碼,編譯成UTF-8 encoding的二進制碼(即.class),這是JVM對常數字串在二進制碼(java encoding)內表達的規范。Phontol.com

          第三階段是Tomcat(或其的application container)載入和執行階段二的來的JAVA二進制碼,輸出的結果,也就是在客戶端見到的,這時隱藏在階段一和階段二的參數contentType就發揮了功效

          contentType的設定.

          pageEncoding 和contentType的預設都是 ISO8859-1. 而隨便設定了其中一個, 另一個就跟著一樣了(TOMCAT4.1.27是如此). 但這不是絕對的, 這要看各自JSPC的處理方式. 而pageEncoding不等于contentType, 更有利亞洲區的文字 CJKV系JSP網頁的開發和展示, (例pageEncoding=GB2312 不等于 contentType=utf-8)。


          在Tomcat中如果在jsp中設定了pageEncoding,則contentType也跟著設定成相同的編碼了,但是在resion中就不是,resin中還會用默認的,這點通過查看編譯后的類servlet java文件就可以看到這一點,而問題恰恰就出在這里,所以,在jsp中,如果是在resin下最好還是明確的單獨設定這2個屬性。


          jsp文件不像.java,.java在被編譯器讀入的時候默認采用的是操作系統所設定的locale所對應的編碼,比如中國大陸就是GBK,臺灣就是BIG5或者MS950。Phontol.com而一般我們不管是在記事本還是在ue中寫代碼,如果沒有經過特別轉碼的話,寫出來的都是本地編碼格式的內容。Phontol.com所以編譯器采用的方法剛好可以讓虛擬機得到正確的資料。Phontol.com

          但是jsp文件不是這樣,它沒有這個默認轉碼過程,但是指定了pageEncoding就可以實現正確轉碼了。Phontol.com

          舉個例子:

          <%@ page contentType="text/html;charset=utf-8" %>

          大都會打印出亂碼,因為輸入的“你好”是gbk的,但是服務器是否正確抓到“你好”不得而知。Phontol.com

          但是如果更改為

          <%@ page contentType="text/html;charset=utf-8" pageEncoding="GBK"%>

          這樣就服務器一定會是正確抓到“你好”了。Phontol.com


          posted @ 2008-10-31 15:21 lqx 閱讀(618) | 評論 (0)編輯 收藏

          linux 下用 perl 發email

          首先,在linux上安裝perl-Mail-Sendmail-0.79-1.0.rh9.rf.noarch.rpm

          perl 代碼如下:

          #
          !/usr/bin/perl 
          use Mail::Sendmail; 
          $delay = 1;
          $f_list="list.txt";
          $line = 0;#skip the column title line
          my $subject="xxx";
          open(FILE,$f_list|| die "Can not open list file\n";
          while(<FILE>){
          chomp;
          $line=$line+1;
          next if($line==1);

          (
          $email,$passwd,$username,$yonghuming= split(/,/);


          %mail = (
              from 
          => 'xxx@xxx.com',
              to 
          => $email,
              subject 
          => $subject,
              
          'content-type' => 'text/html; charset="gbk"',
                  );
          $mail{body} = <<END_OF_BODY;
          <!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=gbk">
          <title>個人郵箱登陸</title>
          <style type="text/css">
          <!--
          body {
              margin
          -left: 0px;
              margin
          -top: 0px;
              margin
          -right: 0px;
              margin
          -bottom: 0px;
          }
          -->
          </style>
          <link href="images/css.css" rel="stylesheet" type="text/css">
          <style type="text/css">
          <!--
          .style1 {font-size: 13px}
          .style3 {color: #0066CC}
          .style4 {color: #FF0000}
          -->
          </style>
          </head>

          <body>
          <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
            
          <tr>
              
          <td height="10" valign="bottom"><hr width="100%" size="10" color="#3399FF">test</td>
            
          </tr> 
          </table>
          </body>
          </html>

          END_OF_BODY

          sendmail(
          %mail|| print "Error: $Mail::Sendmail::error\n";
          sleep($delay); 
          }
          close(FILE);


          list file 內容格式:
          xx@163.com,xdf.com,xxx,xxx

          posted @ 2008-10-17 09:33 lqx 閱讀(381) | 評論 (0)編輯 收藏

          firefox 3 call Components.classes

          在firefox3下Components.classes 是不允許直接調用的,需要加上如下那句粗體的語句才可以
          <script>
              netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
              var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                  prefs.setBoolPref("dom.allow_scripts_to_close_windows", true);
          </script>

          posted @ 2008-09-18 13:16 lqx 閱讀(1018) | 評論 (0)編輯 收藏

          firefox extension 中 call click 事件


          在chomal.manifest里如下設定,注意黃色部分,很關鍵
          content     sample    chrome/content/  xpcnativewrappers=no
          overlay chrome://browser/content/browser.xul chrome://sample/content/sample.xul

          調用方式:
          window.content.document.getElementById('sssddd').onclick();


          參考如下連接

          http://developer.mozilla.org/En/Safely_accessing_content_DOM_from_chrome

          xpcnativewrappers

          http://developer.mozilla.org/en/Chrome_Registration

          posted @ 2008-09-17 13:44 lqx 閱讀(301) | 評論 (0)編輯 收藏

          div 底端對齊

          <style type="text/css" media="all">
          div,img
          {margin: 0;padding: 0;border: 0;}


          #content
          {width: 303px;height: 404px;background: #F63;color: #000;font: 12px Arial,Helvetica,sans-serif;position: relative;}

          #content div
          {position: absolute;left: 0;bottom: 0;}
          </style>
          </head>
          <body>
          <div id="content">
          <div>底端對齊 </div>
          </div>

          posted @ 2008-09-01 13:13 lqx 閱讀(875) | 評論 (0)編輯 收藏

          如何把表1的數據換成表2的形式







          select  max(a.num) A,max(b.num) B,max(c.num) C,tttt.name from tttt
          left join (select * from tttt where abc='C') c on c.abc=tttt.abc and c.name=tttt.name
          left join (select * from tttt where abc='B') b on b.abc=tttt.abc and b.name=tttt.name
          left join (select * from tttt where abc='A') a on a.abc=tttt.abc and a.name=tttt.name
          group by name

          posted @ 2008-07-16 11:18 lqx 閱讀(116) | 評論 (0)編輯 收藏

          【轉】JAVA的內省(introspector)與反射(reflection)

           很多朋友在深入的接觸 JAVA 語言后就會發現這樣兩個詞:反射 (Reflection) 和內省 (Introspector) ,經常搞不清楚這到底是怎么回事,在什么場合下應用以及如何使用?今天把這二者放在一起介紹,因為它們二者是相輔相成的。

          反射

          相對而言,反射比內省更容易理解一點。用一句比較白的話來概括,反射就是讓你可以通過名稱來得到對象 ( 類,屬性,方法 ) 的技術。例如我們可以通過類名來生成一個類的實例;知道了方法名,就可以調用這個方法;知道了屬性名就可以訪問這個屬性的值。

          還是寫兩個例子讓大家更直觀的了解反射的使用方法:
          引用
          //通過類名來構造一個類的實例
          Class cls_str = Class.forName( "java.lang.String" );
          // 上面這句很眼熟,因為使用過 JDBC 訪問數據庫的人都用過 J
          Object str = cls_str.newInstance();
          // 相當于 String str = new String();
          //通過方法名來調用一個方法
          String methodName = "length" ;
          Method m = cls_str.getMethod(methodName, null );
          System.out.println( "length is " + m.invoke(str, null ));
          // 相當于 System.out.println(str.length());

          上面的兩個例子是比較常用方法。看到上面的例子就有人要發問了:為什么要這么麻煩呢?本來一條語句就完成的事情干嗎要整這么復雜?沒錯,在上面的例子中確實沒有必要這么麻煩。不過你想像這樣一個應用程序,它支持動態的功能擴展,也就是說程序不重新啟動但是可以自動加載新的功能,這個功能使用一個具體類來表示。首先我們必須為這些功能定義一個接口類,然后我們要求所有擴展的功能類必須實現我指定的接口,這個規定了應用程序和可擴展功能之間的接口規則,但是怎么動態加載呢?我們必須讓應用程序知道要擴展的功能類的類名,比如是 test.Func1 ,當我們把這個類名 ( 字符串 ) 告訴應用程序后,它就可以使用我們第一個例子的方法來加載并啟用新的功能。這就是類的反射,請問你有別的選擇嗎?

          關于方法的反射建議大家看我的另外一篇文章《 利用 Turbine 的事件映射來擴展 Struts 的功能 》,地址是: http://www.javayou.com/article/CSDN/extend_struts.html 。這篇文章詳細介紹了如果通過反射來擴展 Struts 框架的功能。

          內省

          內省是 Java 語言對 Bean 類屬性、事件的一種缺省處理方法。例如類 A 中有屬性 name, 那我們可以通過 getName,setName 來得到其值或者設置新的值。通過 getName/setName 來訪問 name 屬性,這就是默認的規則。 Java 中提供了一套 API 用來訪問某個屬性的 getter/setter 方法,通過這些 API 可以使你不需要了解這個規則(但你最好還是要搞清楚),這些 API 存放于包 java.beans 中。


          一般的做法是通過類 Introspector 來獲取某個對象的 BeanInfo 信息,然后通過 BeanInfo 來獲取屬性的描述器( PropertyDescriptor ),通過這個屬性描述器就可以獲取某個屬性對應的 getter/setter 方法,然后我們就可以通過反射機制來調用這些方法。下面我們來看一個例子,這個例子把某個對象的所有屬性名稱和值都打印出來:


          引用
          /*
          * Created on 2004-6-29
          */

          package demo;


          import java.beans.BeanInfo;
          import java.beans.Introspector;
          import java.beans.PropertyDescriptor;


          /**
          * 內省演示例子
          * @author liudong
          */

          public class IntrospectorDemo {
          String name;
          public static void main(String[] args) throws Exception{
          IntrospectorDemo demo = new IntrospectorDemo();
          demo.setName( "Winter Lau" );

          // 如果不想把父類的屬性也列出來的話,
          // 那 getBeanInfo 的第二個參數填寫父類的信息
          BeanInfo bi = Introspector.getBeanInfo(demo.getClass(), Object. class );
          PropertyDescriptor[] props = bi.getPropertyDescriptors();
          for ( int i=0;i<props.length;i++){
          System.out.println(props[i].getName()+ "=" +
          props[i].getReadMethod().invoke(demo, null ));
          }

          }

          public String getName() {
          return name;
          }

          public void setName(String name) {
          this .name = name;
          }
          }


          Web 開發框架 Struts 中的 FormBean 就是通過內省機制來將表單中的數據映射到類的屬性上,因此要求 FormBean 的每個屬性要有 getter/setter 方法。但也并不總是這樣,什么意思呢?就是說對一個 Bean 類來講,我可以沒有屬性,但是只要有 getter/setter 方法中的其中一個,那么 Java 的內省機制就會認為存在一個屬性,比如類中有方法 setMobile ,那么就認為存在一個 mobile 的屬性,這樣可以方便我們把 Bean 類通過一個接口來定義而不用去關心具體實現,不用去關心 Bean 中數據的存儲。比如我們可以把所有的 getter/setter 方法放到接口里定義,但是真正數據的存取則是在具體類中去實現,這樣可提高系統的擴展性。

          總結
          將 Java 的反射以及內省應用到程序設計中去可以大大的提供程序的智能化和可擴展性。有很多項目都是采取這兩種技術來實現其核心功能,例如我們前面提到的 Struts ,還有用于處理 XML 文件的 Digester 項目,其實應該說幾乎所有的項目都或多或少的采用這兩種技術。在實際應用過程中二者要相互結合方能發揮真正的智能化以及高度可擴展性。

          另外,以下是SUN的java doc 對Introspector的解釋:
          public class Introspector
          extends Object

          The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.

          For each of those three kinds of information, the Introspector will separately analyze the bean's class and superclasses looking for either explicit or implicit information and use that information to build a BeanInfo object that comprehensively describes the target bean.

          For each class "Foo", explicit information may be available if there exists a corresponding "FooBeanInfo" class that provides a non-null value when queried for the information. We first look for the BeanInfo class by taking the full package-qualified name of the target bean class and appending "BeanInfo" to form a new class name. If this fails, then we take the final classname component of this name, and look for that class in each of the packages specified in the BeanInfo package search path.

          Thus for a class such as "sun.xyz.OurButton" we would first look for a BeanInfo class called "sun.xyz.OurButtonBeanInfo" and if that failed we'd look in each package in the BeanInfo search path for an OurButtonBeanInfo class. With the default search path, this would mean looking for "sun.beans.infos.OurButtonBeanInfo".

          If a class provides explicit BeanInfo about itself then we add that to the BeanInfo information we obtained from analyzing any derived classes, but we regard the explicit information as being definitive for the current class and its base classes, and do not proceed any further up the superclass chain.

          If we don't find explicit BeanInfo on a class, we use low-level reflection to study the methods of the class and apply standard design patterns to identify property accessors, event sources, or public methods. We then proceed to analyze the class's superclass and add in the information from it (and possibly on up the superclass chain).

          Because the Introspector caches BeanInfo classes for better performance, take care if you use it in an application that uses multiple class loaders. In general, when you destroy a ClassLoader that has been used to introspect classes, you should use the Introspector.flushCaches or Introspector.flushFromCaches method to flush all of the introspected classes out of the cache.

          For more information about introspection and design patterns, please consult the JavaBeans specification.

          posted @ 2008-07-14 17:21 lqx 閱讀(373) | 評論 (0)編輯 收藏

          [收藏]有關java I/O流的問題

          FileInputStream 和 FileReader(頭ho暈的)
          FileReader 會做編碼轉換,FileInputStream會忠實于原始文件數據。任何形式的Reader都會涉及編碼。

          BufferedInputStream和BufferedOutputStream
          BufferedInputStream: 添加了功能,即緩沖輸入和支持 mark 和 reset 方法的能力。創建 BufferedInputStream 時即創建了一個內部緩沖區數組。讀取或跳過流中的各字節時,必要時可根據所包含的輸入流再次填充該內部緩沖區,一次填充多個字節。mark 操作記錄輸入流中的某個點,reset 操作導致在從所包含的輸入流中獲取新的字節前,再次讀取自最后一次 mark 操作以來所讀取的所有字節。
          BufferedOutputStream:該類實現緩沖的輸出流。通過設置這種輸出流,應用程序就可以將各個字節寫入基礎輸出流中,而不必為每次字節寫入調用基礎系統。

          BufferedReader和FileReader
          BufferedReader :由Reader類擴展而來,提供通用的緩沖方式文本讀取,而且提供了很實用的readLine,讀取分行文本很適合,BufferedReader是針對Reader的,不直接針對文件,也不是只針對文件讀取。 
          FileReader 是由java.io.InputStreamReade擴展來的,是針對文件讀取的。實際使用時往往用   BufferedReader   bufferedreader   =   new   BufferedReader(new   FileReader("test.conf"));先建立一個文件reader,再用BufferedReader讀。  
          FileInputStream和Reader 
          FileInputStream: 擴展自java.io.InputStream,InputStream提供的是字節流的讀取,而非文本讀取,這是和Reader類的根本區別。用 Reader讀取出來的是char數組或者String   ,使用InputStream讀取出來的是byte數組。  
          Reader:Reader 類及其子類提供的字符流的讀取char(16位),InputStream及其子類提供字節流的讀取byte(8位),所以FileReader類是將文 件按字符流的方式讀取,FileInputStream則按字節流的方式讀取文件,BufferedReader的作用是提供緩沖, InputStreamReader可以將讀如stream轉換成字符流方式(即reader)是reader和stream之間的橋梁

          BufferedInputStream和BufferedOutputStream的一個例子
          import java.io.*;

          public class BufferedStreamDemo...{
              public static void main(String[] args)...{
                  try...{
                      byte[] data=new byte[1];
                     
                      File srcFile=new File("BufferedStreamDemo.java");
                      File desFile=new File("BufferedStreamDemo.txt");
                     
                      BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(srcFile));
                      BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(desFile));
                     
                      System.out.println("復制文件: "+srcFile.length()+"字節");
                     
                      while(bufferedInputStream.read(data)!=-1)...{
                          bufferedOutputStream.write(data);
                      }
                     
                      //將緩沖區中的數據全部寫出
                      bufferedOutputStream.flush();
                     
                      System.out.println("復制完成");
                     
                      //顯示輸出BufferedStreamDemo.txt文件的內容
                      bufferedInputStream =new BufferedInputStream(new FileInputStream(new File("BufferedStreamDemo.txt")));
                      while(bufferedInputStream.read(data)!=-1)...{
                          String str=new String(data);
                          System.out.print(str);
                      }
                     
                      bufferedInputStream.close();
                      bufferedOutputStream.close();           
                     
                  }catch(ArrayIndexOutOfBoundsException e)...{
                      System.out.println("using: java useFileStream src des");
                      e.printStackTrace();
                  }catch(IOException e)...{
                      e.printStackTrace();
                  }
              }
          }

          posted @ 2008-07-09 17:04 lqx 閱讀(262) | 評論 (0)編輯 收藏

          僅列出標題
          共18頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 
          主站蜘蛛池模板: 五莲县| 双鸭山市| 托克托县| 石河子市| 舒兰市| 鄱阳县| 咸阳市| 曲水县| 绥江县| 凤山县| 巫溪县| 聊城市| 牙克石市| 云霄县| 辽阳市| 志丹县| 大埔县| 义马市| 保德县| 旬阳县| 宣化县| 广安市| 东丰县| 年辖:市辖区| 西平县| 六盘水市| 广昌县| 长沙市| 延庆县| 辽源市| 旌德县| 塔河县| 广昌县| 东源县| 潼南县| 大冶市| 商水县| 延寿县| 泽州县| 宝鸡市| 梁山县|