javaGrowing

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  92 随笔 :: 33 文章 :: 49 评论 :: 0 Trackbacks

2007年5月19日 #

单击“开始→运行”,在“打开”框中键入“MOUNTVOL   /E”,然后单击“确定”按钮,重新启动电脑。
posted @ 2008-11-01 15:19 javaGrowing 阅读(511) | 评论 (0)编辑 收藏

Archetype Command
JSF Basic mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-jsf -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Spring MVC Basic mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-spring -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Struts 2 Basic mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Tapestry Basic mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-tapestry -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
JSF Modular mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-jsf -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Spring MVC Modular mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-spring -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Struts 2 Modular mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-struts -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Tapestry Modular mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-tapestry -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject
Core
(backend only)
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-core -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=com.mycompany.app -DartifactId=myproject

You should be able to run AppFuse immediately if you have a MySQL 5.x database installed and it's accessible to root using no password. If you'd prefer to use an embedded database, we've recently added Database Profiles for H2, HSQLDB, etc.

MySQL Security
Running MySQL using root with no password is not the most secure thing to do. Once your database is created, you can change the root password using the command below:
mysql --user=root --pass='' mysql -e "update user set password=password('newpw') where user='root'; flush privileges;"

AppFuse uses the username "root" and a blank password by default. To change these values, modify the <jdbc.username> and <jdbc.password> properties in your project's pom.xml (at the bottom).

Run your application

Running AppFuse is easy now. Once the archetype project is created, Maven will create and populate your database using the hibernate3 and dbunit plugins, respectively. All you have to do is use Maven to run the Jetty container and view your application.

  1. Check your new project into source control, unless you have a good reason not to. Google Code has free Subversion hosting, as do many others.
  2. From the command line, cd into your new project's directory and run mvn to download JARs, Tomcat and run the integration tests in your project. Now is a good time to take a coffee break or grab a beer - downloading everything and running the tests can take 5-10 minutes.
  3. To view your application run mvn jetty:run-war from your project's directory (for a modular project, you'll need to run mvn jetty:run-war from your project's web directory). Maven will start Jetty and you should be able to view your application in your browser at http://localhost:8080.
    The default username/password for an admin user is admin/admin. For a regular user, use user/user.
  4. To override files from AppFuse, run mvn war:inplace. This will extract the dependent WARs into src/main/webapp, where you can change files to your heart's content. When you have the war expanded in your source tree, you can run mvn jetty:run. This will allow you to change files on-the-fly and Jetty will reload them as needed. The only problem with this approach is you end up with an "exploded AppFuse" in your project, which won't bode well for upgrading. We recommend you check your project into source control before running mvn war:inplace. That way, it'll be easier for you to decide what needs to be checked in (over written) and what can be deleted.

If you receive OutOfMemory errors when using mvn jetty:run, see this mailing list thread.

You can change AppFuse from its "embedded mode" to full-source mode by running mvn appfuse:full-source from your project's root directory.

Bug with Ant 1.7.0
Local repositories and projects on Windows platforms are held in directories whose paths contain no spaces. To fix this issue, modify your $M2_HOME/conf/settings.xml and change your localRepository to something like the following:
<localRepository>c:\docume~1\username\.m2\repository</localRepository> 
Development Environment
See development environment for detailed instructions on how to setup your computer to develop AppFuse-based applications.
Changing database settings
To change your MySQL database settings, simply change the <jdbc.*> properties at the bottom of your pom.xml. See Database Profiles to use a database other than MySQL


set MAVEN_OPTS=-Xmx512m -Xms512m -XX:MaxPermSize=512m
posted @ 2008-09-03 18:07 javaGrowing 阅读(469) | 评论 (0)编辑 收藏

今天我在调程序时,遇到了见郁闷的事,我用ajax从前台显示页面传字符串变量给处理页面,出现了件怪事,我想传的内容是abc+,但接收以后用System.out.println()输出是abc空格。反复测试好几遍,都是这个结果,真是令人郁闷。在网上搜了搜,找到了原因。
原因:   url中有些字符被转义,比如空格被编码成加号,于是传的参数明明是加号,获取的值却成了空格。如何解决呢?如果是通过url传递参数,应该对其进行必要的编码。
解决办法:
         在javascript中加入   function URLencode(sStr)
{
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}
对字符串进行处理.如:var str=URLencode("abc+")
posted @ 2007-12-19 09:02 javaGrowing 阅读(11275) | 评论 (5)编辑 收藏

The following processes including SD,MM,PP,FI,CO .


SD Relevant Steps:
1.Check if all delivery in due list have been processedVL06G (Could set background jobs to automatically generate the billing due list for posting)
2.Check if all picking in due list have been processedVL06P
3.Check if all goods issue in due list have been processedVL04
4.Check if all billed AR has been release to accountingVFX3

PP Relevant Steps:
1.Check if all back flash error has been solved (COGI Check every day this kind of problem and solved on time)
2.Check if all production orders that will not be followed have been finally  confirmed or technically closed. Do final confirmation even small quantity  variance exist but we consider this order has finished
3.Do technical close at month end for the orders will not be followed in the future
4.Confirmation. Do not confirm any assembly scrap when do production order conf.

MM Relevant Steps
1.Check if all goods movement has been booked in SAP
2.GR/IR clearing accounts maintenance if needed
3.Open the MM period for movement posting when new period starts MMPV

FI Relevant Steps
1.Book all accounting entries for:
- Accrued expense
- Do recurring entry (prepayment, accrued expense, amortization)
- Process G/L, vendor, customer balance & open items
- Cost allocations by FI
2.AM Fixed asset depreciation run and period postingAFAB
3.AM Periodic posting ASKB
4.G/L,AR,AP balance check & Open item clearing
5.Foreign currency revaluation for bank/cash & AR AP open items
Bank/cash revaluation: F.06
AR AP open items: F.05
6.GR/IR clearing account regrouping process F.19
7.Automatic clearing for G/L accounts, vendor and customer F.13

CO Relevant Steps
1.Release standard cost estimate
CK24Only when there are costing run to be released.
2.Cost reallocation according to activity
Distribution: CKV5
Assessment: KSU5
3.Cost splitting among activity KSS2
4.Actual activity price calculation KSII
5.Production order revaluation with actual activity price CON2
6.WIP calculationfor the production orders KKAO
7.Variance calculation for the production orders KKS1
8.Production / Costing orders settlement CO88/KO88
9.Material ledger closing > MMPV first
a.Create costing runCKRU00
b.Allow settlementCKMF_RUN
c.Allow closing entriesCKMG_RUN
d.Material selectionCKMB_RUN
e.Determine costing sequenceCKMC_RUN
f.Settle single levelCKMH_RUN
g.Settle multi levelCKMM_RUN
h.Post closingCKMI
posted @ 2007-12-18 16:03 javaGrowing 阅读(893) | 评论 (0)编辑 收藏

所谓排序,就是要整理文件中的记录,使之按关键字递增(或递减)次序排列起来。其确切定义如下:
  输入:n个记录R1,R2,…,Rn,其相应的关键字分别为K1,K2,…,Kn
  输出:Ril,Ri2,…,Rin,使得Ki1≤Ki2≤…≤Kin。(或Ki1≥Ki2≥…≥Kin)。

    这里,我们简单介绍几种排序方法,直接插入排序、希儿排序、冒泡排序、快速排序、直接选择排序,文中所提及的代码在IE6下测试通过。

直接插入排序基本思想
    假设待排序的记录存放在数组R[1..n]中。初始时,R[1]自成1个有序区,无序区为R[2..n]。从i=2起直至i=n为止,依次将R[i]插入当前的有序区R[1..i-1]中,生成含n个记录的有序区。

    算法描述

 function InsertSort(arr) { //插入排序->直接插入法排序
  var st = new Date();
  
var temp, j;
  
for(var i=1; i<arr.length; i++) {
   
if((arr[i]) < (arr[i-1])) {
    temp 
= arr[i];
    j 
= i-1;
    
do {
     arr[j
+1= arr[j];
     j
--;
    }
    
while (j>-1 && (temp) < (arr[j]));
    arr[j
+1= temp;
   }
//endif
  }
  status 
= (new Date() - st) + ' ms';
  
return arr;
 }

希尔排序基本思想
   先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。所有距离为dl的倍数的记录放在同一个组中。先在各组内进行直接插人排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
   该方法实质上是一种分组插入方法。

    算法描述

 

function ShellSort(arr) { //插入排序->希儿排序
  var st = new Date();
  
var increment = arr.length;
  
do {
   increment 
= (increment/3|0+ 1;
   arr 
= ShellPass(arr, increment);
  }
  
while (increment > 1)

  status 
= (new Date() - st) + ' ms';
  
return arr;
 }
 
function ShellPass(arr, d) { //希儿排序分段执行函数
  var temp, j;
  
for(var i=d; i<arr.length; i++) {
   
if((arr[i]) < (arr[i-d])) {
    temp 
= arr[i]; j = i-d;
    
do {
     arr[j
+d] = arr[j];
     j 
= j-d;
    }
    
while (j>-1 && (temp) < (arr[j]));
    arr[j
+d] = temp;
   }
//endif
  }
  
return arr;
 }

冒泡排序基本思想
    将被排序的记录数组R[1..n]垂直排列,每个记录R[i]看作是重量为R[i].key的气泡。根据轻气泡不能在重气泡之下的原则,从下往上扫描数组 R:凡扫描到违反本原则的轻气泡,就使其向上"飘浮"。如此反复进行,直到最后任何两个气泡都是轻者在上,重者在下为止。

    算法描述
 

function BubbleSort(arr) { //交换排序->冒泡排序
  var st = new Date();
  
var temp;
  
var exchange;
  
for(var i=0; i<arr.length; i++) {
   exchange 
= false;
   
for(var j=arr.length-2; j>=i; j--) {
    
if((arr[j+1]) < (arr[j])) {
     temp 
= arr[j+1];
     arr[j
+1= arr[j];
     arr[j] 
= temp;
     exchange 
= true;
    }
   }
   
if(!exchange) break;
  }
  status 
= (new Date() - st) + ' ms';
  
return arr;
 }

快速排序基本思想
    将原问题分解为若干个规模更小但结构与原问题相似的子问题。递归地解这些子问题,然后将这些子问题的解组合为原问题的解。
    在R[low..high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low..pivotpos- 1)和R[pivotpos+1..high],并使左边子区间中所有记录的关键字均小于等于基准记录(不妨记为pivot)的关键字 pivot.key,右边的子区间中所有记录的关键字均大于等于pivot.key,而基准记录pivot则位于正确的位置(pivotpos)上,它无 须参加后续的排序。

    算法描述
 

function QuickSort(arr) { //交换排序->快速排序
  if (arguments.length>1) {
   
var low = arguments[1];
   
var high = arguments[2];
  } 
else {
   
var low = 0;
   
var high = arr.length-1;
  }
  
if(low < high){
   
// function Partition
   var i = low;
   
var j = high;
   
var pivot = arr[i];
   
while(i<j) {
    
while(i<&& arr[j]>=pivot)
     j
--;
    
if(i<j)
     arr[i
++= arr[j];
    
while(i<&& arr[i]<=pivot)
     i
++;
    
if(i<j)
     arr[j
--= arr[i];
   }
//endwhile
   arr[i] = pivot;
   
// end function
   var pivotpos = i; //Partition(arr,low,high);
   QuickSort(arr, low, pivotpos-1);
   QuickSort(arr, pivotpos
+1, high);
  } 
else
   
return;
   
return arr;
 }

直接选择排序基本思想
   n个记录的文件的直接选择排序可经过n
-1趟直接选择排序得到有序结果:
 ①初始状态:无序区为R[
1..n],有序区为空。
 ②第1趟排序
    在无序区R[
1..n]中选出关键字最小的记录R[k],将它与无序区的第1个记录R[1]交换,使R[1..1]和R[2..n]分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。
  ……
 ③第i趟排序
  第i趟排序开始时,当前有序区和无序区分别为R[
1..i-1]和R[i..n](1≤i≤n-1)。该趟排序从当前无序区中选出关键字最小的记录R [k],将它与无序区的第1个记录R[i]交换,使R[1..i]和R[i+1..n]分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。
    这样,n个记录的文件的直接选择排序可经过n
-1趟直接选择排序得到有序结果。

    算法描述
 
function SelectSort(arr) { //选择排序->直接选择排序
  var st = new Date();
  
var temp;
  
for(var i=0; i<arr.length; i++) {
   
var k = i;
   
for(var j=i+1; j<arr.length; j++) {
    
if((arr[j]) < (arr[k]))
     k 
= j;
   }
   
if (k != i){
    temp 
= arr[i];
    arr[i] 
= arr[k];
    arr[k] 
= temp;
   }
  }
  status 
= (new Date() - st) + ' ms';
  
return arr;
 }

posted @ 2007-07-23 16:23 javaGrowing 阅读(489) | 评论 (0)编辑 收藏

Dom + Javascript 在本机浏览器中对的表格数据进行排序

在本机浏览器中对的表格数据进行排序
在Web应用中,数据从服务器端返回到客户端,以表格形式表现出来。如果要对数据集按指定的列排序显示,常规做法都是向服务器发出请求,服务器端程序重新从数据库中取出按指定列排序的数据,返回给客户端,页面重新显示排序后数据。
 
采用这种方式有如下缺点:
1- 响应时间延迟,每次排序都要向服务器端发送请求,等待结果返回,同时增加网络负载。
2- 编程复杂,可维护性差,而且客户端和服务器端代码耦合度很高,客户端和服务器端都要处理排序涉及的列名、排序方式,如果有分页和查询条件,都需要在客户端页面中保留,排序请求时重新传递到服务器端,当参数数量很多时极易出错。
3- 重用度很低,针对不同表格,很难抽象出一个公共程序来共用,需要逐个编写代码实现,增加工作量。
 
现在换一个角度考虑,数据既然已下载到了客户端,在重新排序时没有必要再重服务器端获取,只要对浏览器中的数据重新排序显示就可以了。要实现该目标,需要做到以下几点:
1 - 获得表格中要排序的数据,将其放入一个2维数组中。
2 - 对2维数组排序。
3 - 用排序后的数据重新更新表格。
 
利用浏览器支持的DOM(Document Object Model)和JavaScript即可实现上述目标。
 
通常页面中会很多的<table>,要获得需要排序数据所在的table,需要在<table>中增加一个id属性,便于document对象用getElementById得到该表格对象,例如要排序的表格定义如下:
 <table id="st" >
    <tr>
        <td>1</td>
        <td>2</td>
    </tr>
</table>
 
在javascript中,用var objTable = document.getElementById("st")就可得到表格对象,该对象在DOM中定义为一个Element。
 
然 后用 var objRows = objTable.getElementsByTagName("tr")得到该表格中全部的行对象,ObjRows.length返回该表格行数; var rowi = objRows[i].getElementsByTagName("td")得到第i行的全部<td>节点,i从0开始计数, rowi.item(j)则可得到第i行,第j列的节点,该节点的innerHTML为该节点<td></td>之间的内容。
 
获取表格数据到2维数组的代码见源代码,此处略。
 
下面说明在javascript中2维数组的构造和排序。
javascript不支持2维数组,因此需要用数组的数组来模拟一个2维数组,其方法是先定义一个1维数组,元素个数为2维数组的行数,然后对每个元素赋一个值,值为一个数组,其元素个数为2维数组的列数。构造代码如下:
 
var rows = new Array(R);  //R为行数
for(var i = 0; i < rows.length; i++){
    rows[i] = new Array(C);    //C为列数
}
 
 
利用javascript中的Array.sort(comparer)对rows中的元素排序,比较方式由重新定义的比较函数得到。要根据第j列元素的大小排序,只要定义如下函数即可:
 
function compareCol(a,b){
   
    if (a[j] < b[j])
        return -1;
 
    if (a[j] > b[j])
        return 1;
 
   return 0;
 
}
 
因为compareCol只能有两个参数,因此j要定义为全局变量。
 
用rows.sort (compareCol)就可实现根据j列值的大小对行进行排序。根据javascript文档,字符串比较大小是按照其Unicode编码的大小来比 较,对英文排序没有问题,对中文排序时就不是按通常的拼音排序,那需要javascript提供本地化支持,目前没有发现javascript此功能。该 功能在java中可用java.text.Collator实现。
 
 
 以上介绍了在本地对浏览器中的数据进行排序的主要思想, 为方便使用,将这些功能进行了封装,以javascript函数的提供,存放在sorttable.js文件中,在需要的页面中用<script type=text/javascript src='sorttable.js'></script>引入。
 
 
 下面说明排序函数原型和使用方法。
 
函数1 function sortTable(tableId,sortCol,compareType)
对页面中指定表格中的数据进行排序,通常第一行为标题行,排序时从第二行开始,第一次调用为升序排列,第二次为降序排列,依次轮换。
tableId    为<table id=''>中id的值,在同一个页面中要唯一。
sortCol    排序时用来比较大小的数据所在的列,从1开始计数。
compareType 排序时比较大小的方式,s-按字符串比较大小,n-按数字比较大小。
 
 
 
 函数2 function sortTableInRange(tableId,sortCol,compareType,startRow,endRow,startCol,endCol)
 
        对表格中指定的区域数据排序,有时数据第一列为流水号,最后一行为合计,这些数据不需要参与排序,可用此函数来对部分数据排序。
       
        tableId    为<table id=''>中id的值,在同一个页面中要唯一。
        sortCol    排序时用来比较大小的数据所在的列,从1开始计数。
        compareType 排序时比较大小的方式,s-按字符串比较大小,n-按数字比较大小。
        startRow,endRow 要排序区域开始和结束行号,从1开始计数。例如对第2行到第7行排序,startRow=2,endRow=7
        startCol,endCol  要排序区域开始和结束列号,从1开始计数。
 
 
 
 
 
存在的问题:
1- 中文不能按拼音排序。
 
 
要注意的问题:
要排序的table必须用ID标示,并要作为参数传给排序函数,表格中的数据应该是可以排序的,否则结果不可预知;要排序的表格不能有嵌套表,否则排序出错。
 
 
本函数已在IE6.0 ,FireFox1.01中运行通过。源代码和例子代码见后。
 
 
 
 
参考资料:
 
Danny Goodman with Michael Morrison   JavaScript Bible 5th  ,John Wiley and Sons  2004
 
David Flanagan  JavaScript The Definitive Guide 4th ,  O'Reilly  2001
 
 
 
 
 
 附源代码:要运行例子,需要将javascript代码保存到sorttable.js文件中,html部分代码保存到同一目录下另一文件中即可。
 
 
sorttable.js
 
 

 1 //=========================================================
 2  //
 3  //  在本机对浏览器页面表格中的数据行进行排序的javascript函数
 4  // 
 5  //  author William  QQ: 22967225
 6 //  create date 2005-12-2
 7  //  version 1.0
 8  //=========================================================
 9 
10  //column index for sort
11  var indexCol;
12  //比较函数,用于Array.sort()排序时比较用。
13  //本函数比较数组元素array1[indexCol]和元素array2[indexCol]Unicode值的大小
14  function arrayCompare(array1,array2){
15   //alert(array1.length+"--"+array1[indexCol]);
16   if (array1[indexCol] < array2[indexCol])
17    return -1;
18   if (array1[indexCol] > array2[indexCol])
19    return 1;
20  
21   return 0;
22  
23  }
24  //比较数组元素array1[indexCol]和元素array2[indexCol]的数值大小
25  function arrayCompareNumber(array1,array2){
26  
27   if (parseInt(array1[indexCol]) < parseInt(array2[indexCol]))
28    return -1;
29   if (parseInt(array1[indexCol]) > parseInt(array2[indexCol]))
30    return 1;
31  
32   return 0;
33  }
34  //与arrayCompare相反方式比较大小,用于倒序使用
35  function arrayCompareRev(array1,array2){
36  
37   if (array1[indexCol] < array2[indexCol])
38    return 1;
39   if (array1[indexCol] > array2[indexCol])
40    return -1;
41  
42   return 0;
43  
44  }
45  //与arrayCompareNumber相反方式比较大小,用于倒序使用
46  function arrayCompareNumberRev(array1,array2){
47   if (parseInt(array1[indexCol]) < parseInt(array2[indexCol]))
48    return 1;
49   if (parseInt(array1[indexCol]) > parseInt(array2[indexCol]))
50    return -1;
51  
52   return 0;
53  }
54  
55  //define a 2-dimension array
56  function BiArray(rows,cols){
57  
58   //simulate multidimension array
59   this.rows = rows;
60   this.cols = cols;
61  
62   //construct array
63   var lines = new Array(rows);
64   for(var i = 0;i < lines.length; i++){
65    lines[i] = new Array(cols);
66   }

 
posted @ 2007-07-23 16:20 javaGrowing 阅读(422) | 评论 (0)编辑 收藏

function toHtml(strBody,cset)
{
 var Rec=new ActiveXObject("ADODB.RecordSet");
 Rec.Fields.Append("DDD",201,1);
 Rec.Open();
 Rec.AddNew();

 Rec(0).AppendChunk(strBody);

 Rec.Update();
 var HTML=Rec(0).Value;
 Rec.Close();
 delete Rec;
 document.charset=cset;
 return(HTML);
}

posted @ 2007-05-26 01:07 javaGrowing 阅读(983) | 评论 (0)编辑 收藏

出处:http://www.smallrain.net/study_show.asp?id=703

<div id="Error"></div>
<div id="State"></div>
<div id="DownloadEnd"></div>
<Script Language="JavaScript">
<!--
// more javascript from http://www.smallrain.net

function Ajax(OnError,OnState,OnDownloadEnd)
{
 // 错误字符串
 this.ErrorStr   = null;
 // 错误事件驱动,当发生错误时触发
 this.OnError   = OnError;
 // 状态事件驱动,当状态改变时触发
 this.OnState   = OnState;
 // 完成事件驱动,当类操作完成时触发
 this.OnDownloadEnd  = OnDownloadEnd;

 // XMLHTTP 发送数据类型 GET 或 POST
 this.method  = "GET";
 // 将要获取的URL地址
 this.URL  = null;
 // 指定同步或异步读取方式(true 为异步,false 为同步)
 this.sync  = true;
 // 当method 为 POST 时 所要发送的数据
 this.PostData  = null
 // 返回读取完成后的数据
 this.RetData   = null;

 // 创建XMLHTTP对像
 this.HttpObj   = this.createXMLHttpRequest();
 if(this.HttpObj == null)
 {
  // 对像创建失败时中止运行
  return;
 }

 var Obj = this;
 // 调用事件检测
 this.HttpObj.onreadystatechange = function()
 {
  Ajax.handleStateChange(Obj);
 }
}

// UTF 转入 GB (by:Rimifon)
Ajax.prototype.UTFTOGB = function(strBody)
{
 var Rec=new ActiveXObject("ADODB.RecordSet");
 Rec.Fields.Append("DDD",201,1);
 Rec.Open();
 Rec.AddNew();
 Rec(0).AppendChunk(strBody);
 Rec.Update();
 var HTML=Rec(0).Value;
 Rec.Close();
 delete Rec;
 return(HTML);
}

// 创建XMLHTTP对像
Ajax.prototype.createXMLHttpRequest = function()
{
 if (window.XMLHttpRequest)
 {
  //Mozilla 浏览器
  return new XMLHttpRequest();
 }
 else if (window.ActiveXObject)
 {
         var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
         for (var i = 0; i < msxmls.length; i++)
         {
                 try
                 {
                         return new ActiveXObject(msxmls[i]);
                 }catch (e){}

  }
 }
 this.ErrorStr = "你的浏览器不支持XMLHttpRequest对象."
 if(this.OnError)
 {
  this.OnError(this.ErrorStr);
 }
     return null;
}

// 发送HTTP请求
Ajax.prototype.send = function()
{

 if (this.HttpObj !== null)
 {
  this.URL = this.URL + "?t=" + new Date().getTime();
  this.HttpObj.open(this.method, this.URL, this.sync);
  if(this.method.toLocaleUpperCase() == "GET")
  {
   this.HttpObj.send(null);
  }
  else if(this.method.toLocaleUpperCase() == "POST")
  {
   this.HttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   this.HttpObj.send(this.PostData);
  }
  else
  {
   this.ErrorStr = "错误的[method]命令."
   if(this.OnError)
   {
    this.OnError(this.ErrorStr);
   }
   return;
  }

  if (this.HttpObj.readyState == 4)
  {
   // 判断对象状态
              if (this.HttpObj.status == 200)
                 {
    this.RetData = this.UTFTOGB(this.HttpObj.responseBody);
    if(this.OnDownloadEnd)
    {
     this.OnDownloadEnd(this.RetData);
    }
                         return;
                 }
   else
   {
    this.ErrorStr = "您所请求的页面有异常."
    if(this.OnError)
    {
     this.OnError(this.ErrorStr);
    }
    return;
   }
  }

 }

}

// 事件检测
Ajax.handleStateChange = function(Obj)
{
 if(Obj.OnState)
 {
  Obj.OnState(Obj.HttpObj.readyState);
 }

 if (Obj.HttpObj.readyState == 4)
 {
  // 判断对象状态
             if (Obj.HttpObj.status == 200)
                {
   Obj.RetData = Obj.UTFTOGB(Obj.HttpObj.responseBody);
   if(Obj.OnDownloadEnd)
   {
    Obj.OnDownloadEnd(Obj.RetData);
   }
                        return;
                }
  else
  {
   Obj.ErrorStr = "您所请求的页面有异常."
   if(Obj.OnError)
   {
    Obj.OnError(Obj.ErrorStr);
   }
   return;
  }
 }
}


// 错误回调事件函数
function EventError(strValue)
{
 document.getElementById("Error").innerHTML = strValue;
}

// 状态回调事件函数
function EventState(strValue)
{
 var strState = null;
 switch (strValue)
 {
     case 0:
  strState = "未初始化...";
  break;

     case 1:
  strState = "开始读取数据...";
  break;

     case 2:
  strState = "读取数据...";
  break;

     case 3:
  strState = "读取数据中...";
  break;

     case 4:
  strState = "读取完成...";
  break;

     default:
  strState = "未初始化...";
  break;
 }
 document.getElementById("State").innerHTML = strState;
}

// 完成回调事件函数
function EventDownloadEnd(strValue)
{
 document.getElementById("DownloadEnd").innerHTML = strValue;
}


// 初始化Ajax对像,引入事件回调函数
var A1 = new Ajax(EventError,EventState,EventDownloadEnd);
// 指定method数据发送类型
A1.method = "GET";
// 指定URL地址
A1.URL = "http://www.smallrain.net/help.htm"
// 指定为异步处理
A1.sync = true;
//发送请求
A1.send();
//-->
</Script> 

posted @ 2007-05-26 00:37 javaGrowing 阅读(639) | 评论 (0)编辑 收藏

ajax代理程序自动判断字符编码

作者:llinzzi 时间: 2006-03-16 文档类型:原创 来自:蓝色理想
浏览统计 total:6402 | year:1609 | Quarter:376 | Month:132 | Week:26 | today:2

由于ajax在跨域的访问上有问题,目前最好的方法是做代理.写了个代理程序和心得.

为了做ajax的代理,研究了下服务器端的xmlhttp并和客户端的ajax中的xmlhttp做了个比较,后台代码是asp的.

服务器端的xmlhttp也就是asp小偷程序,我把代码改成了javascript.

1.在服务器端的xmlhttp.Open("GET",url,false)异步必须是关闭的,而客户端的异步是打开的,这个很好理解.
2.在服务器端的xmlhttp.Responsebody 这里用的是Responsebody而不是ResponseText或ResponseXml,一开始我是用ResponseText,但在函数bytesToBSTR转换编码的时候提示错误,经过比较发现其他的asp小偷程序里的代码都是Responsebody,分析后,发现body返回来的是二进制数据而不是像ResponseText或ResponseXml那样返回字符或dom对象.

ajax的asp代理函数介绍:
send_request(url) ,url为地址

服务器端代码如下带自动判断所有字符编码,已测试 日语 韩语 繁体:

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!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>XMLHTTP</title>
</head>
<%

 

Server.ScriptTimeout=9999999;
function send_request(url) {
 var codedtext;
 http_request = Server.CreateObject("Microsoft.XMLHTTP");
 http_request.Open("GET",url,false);
 http_request.Send(null);
 if (http_request.ReadyState == 4){
  //自动判断编码开始
  var charresult = http_request.ResponseText.match(/CharSet=(\S+)\">/i);
  if (charresult != null){
  var Cset = charresult[1];
  }else{Cset = "gb2312"}//对获取不到的网站采用gb2312编码,可自行更改
  
//自动判断编码结束
  codedtext = bytesToBSTR(http_request.Responsebody,Cset);
  }else{
  codedtext = "Erro";
  }
 return(codedtext);
}

function bytesToBSTR(body,Cset){
var objstream;
objstream = Server.CreateObject("Adodb.Stream");
objstream.Type = 1;
objstream.Mode = 3;
objstream.Open();
objstream.Write(body);
objstream.Position = 0;
objstream.Type = 2;
objstream.Charset = Cset;
bytesToBSTR = objstream.Readtext;
objstream.Close;
return(bytesToBSTR);
}

%>
<body>
<% Response.Write(send_request("http://www.daum.net")) %>
</body>
</html>

posted @ 2007-05-26 00:31 javaGrowing 阅读(417) | 评论 (0)编辑 收藏

一. Input和Output
1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型:
1.1 以字节为导向的stream
以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种类型:
1) input stream:
1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream:把一个String对象作为InputStream
3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream:把多个InputStream合并为一个InputStream
2) Out stream
1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream:把多个OutStream合并为一个OutStream
1.2 以Unicode字符为导向的stream
以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种类型:
1) Input Stream
1) CharArrayReader:与ByteArrayInputStream对应
2) StringReader:与StringBufferInputStream对应
3) FileReader:与FileInputStream对应
4) PipedReader:与PipedInputStream对应
2) Out Stream
1) CharArrayWrite:与ByteArrayOutputStream对应
2) StringWrite:无与之对应的以字节为导向的stream
3) FileWrite:与FileOutputStream对应
4) PipedWrite:与PipedOutputStream对应
以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
1.3 两种不现导向的stream之间的转换
InputStreamReader和OutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream。
2. stream添加属性
2.1 “为stream添加属性”的作用
运用上面介绍的Java中操作IO的API,我们就可完成我们想完成的任何操作了。但通过FilterInputStream和FilterOutStream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。
如果我们要往一个文件中写入数据,我们可以这样操作:
FileOutStream fs = new FileOutStream(“test.txt”);
然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现“先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的API就没有一个能满足我们的需求了。但是通过FilterInputStream和FilterOutStream的子类,为FileOutStream添加我们所需要的功能。
2.2 FilterInputStream的各种类型
2.2.1 用于封装以字节为导向的InputStream
1) DataInputStream:从stream中读取基本类型(int、char等)数据。
2) BufferedInputStream:使用缓冲区
3) LineNumberInputStream:会记录input stream内的行数,然后可以调用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用于编译器开发
2.2.2 用于封装以字符为导向的InputStream
1) 没有与DataInputStream对应的类。除非在要使用readLine()时改用BufferedReader,否则使用DataInputStream
2) BufferedReader:与BufferedInputStream对应
3) LineNumberReader:与LineNumberInputStream对应
4) PushBackReader:与PushbackInputStream对应
2.3 FilterOutStream的各种类型
2.2.3 用于封装以字节为导向的OutputStream
1) DataIOutStream:往stream中输出基本类型(int、char等)数据。
2) BufferedOutStream:使用缓冲区
3) PrintStream:产生格式化输出
2.2.4 用于封装以字符为导向的OutputStream
1) BufferedWrite:与对应
2) PrintWrite:与对应
3. RandomAccessFile
1) 可通过RandomAccessFile对象完成对文件的读写操作
2) 在产生一个对象时,可指明要打开的文件的性质:r,只读;w,只写;rw可读写
3) 可以直接跳到文件中指定的位置
4. I/O应用的一个例子
import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行为单位从一个文件读取数据
BufferedReader in =
new BufferedReader(
new FileReader("F:\\nepalon\\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();

//1b. 接收键盘的输入
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. 从一个String对象中读取数据
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

//3. 从内存取出格式化输入
try{
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}

//4. 输出到文件
try{
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F:\\nepalon\\ TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}

//5. 数据的存储和恢复
try{
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("F:\\nepalon\\ Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("\nThas was pi:writeChars\n");
out2.writeBytes("Thas was pi:writeByte\n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("F:\\nepalon\\ Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
}
catch(EOFException e){
System.out.println("End of stream");
}

//6. 通过RandomAccessFile操作文件
RandomAccessFile rf =
new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}
关于代码的解释(以区为单位):
1区中,当读取文件时,先把文件内容读到缓存中,当调用in.readLine()时,再从缓存中以字符的方式读取数据(以下简称“缓存字节读取方式”)。
1b区中,由于想以缓存字节读取方式从标准IO(键盘)中读取数据,所以要先把标准IO(System.in)转换成字符导向的stream,再进行BufferedReader封装。
2区中,要以字符的形式从一个String对象中读取数据,所以要产生一个StringReader类型的stream。
4区中,对String对象s2读取数据时,先把对象中的数据存入缓存中,再从缓冲中进行读取;对TestIO.out文件进行操作时,先把格式化后的信息输出到缓存中,再把缓存中的信息输出到文件中。
5区中,对Data.txt文件进行输出时,是先把基本类型的数据输出屋缓存中,再把缓存中的数据输出到文件中;对文件进行读取操作时,先把文件中的数据读取到缓存中,再从缓存中以基本类型的形式进行读取。注意in5.readDouble()这一行。因为写入第一个writeDouble(),所以为了正确显示。也要以基本类型的形式进行读取。
6区是通过RandomAccessFile类对文件进行操作。

posted @ 2007-05-19 20:11 javaGrowing 阅读(321) | 评论 (0)编辑 收藏