posts - 495,  comments - 11,  trackbacks - 0
 
  有些人原本一向身体健康,但在连续熬夜数晚后,突然第二天起床会觉得很疲劳,一闭眼就想睡觉,而且会腰酸背痛,但一到晚上精神又好起来!别以为这是小事!根据中医的看法,是因过劳而造成体内器官阴阳失调,就是体内器官起内讧,互相打架,最后造成器官衰竭而死。

  睡眠是身体进行自我调整的时刻,你侵略它的时间,它便侵略你的健康,希望过劳致死的事件可以不再发生。

  晚上9-11点为免疫系统(淋巴)排毒时间,此段时间应安静或听音乐。

  晚间11-凌晨1点,肝的排毒,需在熟睡中进行。

  凌晨1-3点,胆的排毒,亦同。

  凌晨3-5点,肺的排毒。此即为何咳嗽的人在这段时间咳得最剧烈,因排毒动作已走到肺;不应用止咳药,以免抑制废积物的排除。

  凌晨5-7点,大肠的排毒,应上厕所排便。

  早上7-9点,小肠大量吸收营养的时段,应吃早餐。疗病者最好早吃,在6点半前,养生者在7点半前,不吃早餐者应改变习惯,即使拖到9、10点吃都比不吃好。

  半夜至凌晨4点为脊椎造血时段,必须熟睡,不宜熬夜。
posted @ 2007-08-18 17:47 jadmin 阅读(87) | 评论 (0)编辑 收藏
  1. 扎实的基础。数据结构、离散数学、编译原理,这些是所有计算机科学的基础,如果不掌握他们,很难写出高水平的程序。据我的观察,学计算机专业的人比学其他专业的人更能写出高质量的软件。程序人人都会写,但当你发现写到一定程度很难再提高的时候,就应该想想是不是要回过头来学学这些最基本的理论。不要一开始就去学OOP,即使你再精通OOP,遇到一些基本算法的时候可能也会束手无策。

  2. 丰富的想象力。不要拘泥于固定的思维方式,遇到问题的时候要多想几种解决问题的方案,试试别人从没想过的方法。丰富的想象力是建立在丰富的知识的基础上,除计算机以外,多涉猎其他的学科,比如天文、物理、数学等等。另外,多看科幻电影也是一个很好的途径。

  3. 最简单的是最好的。这也许是所有科学都遵循的一条准则,如此复杂的质能互换原理在爱因斯坦眼里不过是一个简单得不能再简单的公式:E=mc2。简单的方法更容易被人理解,更容易实现,也更容易维护。遇到问题时要优先考虑最简单的方案,只有简单方案不能满足要求时再考虑复杂的方案。

  4. 不钻牛角尖。当你遇到障碍的时候,不妨暂时远离电脑,看看窗外的风景,听听轻音乐,和朋友聊聊天。当我遇到难题的时候会去玩游戏,而且是那种极暴力的打斗类游戏,当负责游戏的那部分大脑细胞极度亢奋的时候,负责编程的那部分大脑细胞就得到了充分的休息。当重新开始工作的时候,我会发现那些难题现在竟然可以迎刃而解。

  5. 对答案的渴求。人类自然科学的发展史就是一个渴求得到答案的过程,即使只能知道答案的一小部分也值得我们去付出。只要你坚定信念,一定要找到问题的答案,你才会付出精力去探索,即使最后没有得到答案,在过程中你也会学到很多东西。

  6. 多与别人交流。三人行必有我师,也许在一次和别人不经意的谈话中,就可以迸出灵感的火花。多上上网,看看别人对同一问题的看法,会给你很大的启发。

  7. 良好的编程风格。注意养成良好的习惯,代码的缩进编排,变量的命名规则要始终保持一致。大家都知道如何排除代码中错误,却往往忽视了对注释的排错。注释是程序的一个重要组成部分,它可以使你的代码更容易理解,而如果代码已经清楚地表达了你的思想,就不必再加注释了,如果注释和代码不一致,那就更加糟糕。

  8. 韧性和毅力。这也许是"高手"和一般程序员最大的区别。A good programming is 99% sweat and 1% coffee。高手们并不是天才,他们是在无数个日日夜夜中磨练出来的。成功能给我们带来无比的喜悦,但过程却是无比的枯燥乏味。你不妨做个测试,找个10000以内的素数表,把它们全都抄下来,然后再检查三遍,如果能够不间断地完成这一工作,你就可以满足这一条。
posted @ 2007-08-18 17:40 jadmin 阅读(63) | 评论 (0)编辑 收藏

To laugh often and much;

To win the respect of intelligent people and the affection of children;

To earn the appreciation of honest critics and endure the betrayal of false friends;

To appreciate beauty;

To find the best in others;

To leave the world a bit better, whether by a healthy child, a garden patch or a redeemed social condition;

To know even one life has breathed easier because you have lived;

This is to have succeeded.

posted @ 2007-08-18 17:36 jadmin 阅读(76) | 评论 (0)编辑 收藏

知识在于积累和实践,-正所谓"厚积薄发",我想应该是这个道理吧!

旧的知识需要时刻温习,就像英语,你不看,不接触,迟早有一天会把它忘得干干净净

从今天开始,每天进步一点点,毕竟"学习"是一辈子的事情,马虎不得!呵呵~~~

posted @ 2007-08-18 16:56 jadmin 阅读(69) | 评论 (0)编辑 收藏

  The furthest distance in the world

  Is not between life and death

  But when I stand in front of you

  Yet you don’t know that

  I love you

  The furthest distance in the world

  Is not when i stand in font of you

  Yet you can’t see my love

  But when undoubtedly knowing the love from both

  Yet cannot

  Be togehter

  The furthest distance in the world

  Is not being apart while being in love

  But when plainly can not resist the yearning

  Yet pretending

  You have never been in my heart

  The furthest distance in the world

  Is not

  But using one’s indifferent heart

  To dig an uncrossable river

  For the one who loves you

  世界上最遥远的距离,不是生与死

  而是我就站在你的面前,你却不知道我爱你

  世界上最遥远的距离,不是我站在你面前,你却不知道我爱你

  而是明明知道彼此相爱,却不能在一起

  世界上最遥远的距离,不是明明知道彼此相爱,却不能在一起

  而是明明无法抵挡这股想念,却还得故意装作丝毫没有把你放在心里

  世界上最遥远的距离,不是明明无法抵挡这股想念,却还得故意装作丝毫没有把你放在心里,而是用自己冷漠的心对爱你的人掘了一条无法跨越的沟渠

posted @ 2007-08-18 16:44 jadmin 阅读(60) | 评论 (0)编辑 收藏

1.Compliment three people every day.每天赞美三个人。
2.Watch a sunrise.看日出。
3.Be the first to say "hello".先向别人打招呼。
4.Live beneath your means.靠自己的力量生活。
5.Treat everyone as your want to be treated.像自己希望得到的善待一样善待他人。
6.Never give up on anybody;Miracles happen.不要放弃任何人;奇迹总会发生。
7.Remember someone's name.记住别人的名字。
8 .Pray not for things,but for wisdom and courage.不要乞求得到什么东西,但可以乞求智慧和勇气。
9.Be tough-minded,but tender hearted.意志要坚强,但心地要善良。

10.Be kinder than you have to be.仁慈,再仁慈一点。
11.Don't forget that a person's greatest emotional need is to feel appreciated.不
要忘记一个人最伟大的情感需要是得到别人的欣赏。
12.Keep your promises.履行你的诺言。
13.Show cheerfulness even when you don’t feel it.即使没有快乐的感觉,也要努力表现出来。
14.Remember that overnight success usually takes 15 years.切记所谓的“一夜成名”通常需要付出“15年的努力”。
15.Leave everything better than you found it.让一切变的更加美好。

16.Remember that winners do what losers don't want to do.切记胜利者通常会做失败者不想做的事情。
17.When you arrive at your job in the morning,let the first thing you say brighten everyone's day.早上到办公室时,先说一件能让每个人在一天中感到愉快的事。
18.Don't rain on other people's parades.看到别人的辉煌时,不要抱怨。

19.Don't waste an opportunity to tell someone you love them.抓住一切机会对别人说“我爱你”。
20.Keep some things to yourself and don't promote have by hurting people you love.自己承受一些事情,不要让所爱的人受伤害。

posted @ 2007-08-18 16:21 jadmin 阅读(49) | 评论 (0)编辑 收藏
程序、进程和线程
     程序式计算机指令的集合,它以文件的形式存储在磁盘上
     进程:是一个程序在其自身的地址空间中的一次执行活动
     进程是资源申请、调度和独立运行的单位,因此,它使用系统中的运行资源;而程序不能申请系统资源,不能被系统调度,也不能作为独立运行的单位,因此,它不占有系统地运行资源
     线程:是进程中的一个单一的连续控制流程。一个进程可以拥有多个线程,但至少有一个线程
     线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是和所属进程中的其他线程共享一个存储空间,这使得线程间的通信远较进程简单
     单CPU下某一个时刻只能有一个线程在运行
   Java对多线程的支持
     Java在语言级提供了对多线程程序设计的支持
     实现多线程程序的两种方式:
       (1)从Thread类(java.lang包)继承:A thread is a thread of execution in a program.
       (2)实现Runnable接口
     Java运行时系统实现了一个用于调度线程执行的线程调度器(其他的语言一般是由OS调度的),用于确定某一时刻有哪一个线程在CPU上运行
     在Java技术中,线程通常是抢占式的而不需要时间片分配进程(分配给多个线程相等的CPU时间的进程)。抢占式调度模型就是许多线程处于可以运行状态(等待状态),但实际上只有一个线程在运行。该线程一只运行到它终止,进入可运行状态(等待状态),或者另一个具有更高优先级的线程变成可运行状态。在后一种情况下,低优先级的线程被高优先级的线程抢占,高优先级的线程获得运行的机会
     Java线程调度器支持不同优先级线程的抢占方式,但其本身不支持相同优先级线程的时间片轮换
     Java运行时系统所在的操作系统(例如:Windows2000)支持时间片的轮换,则线程调度器就支持相同优先级线程的时间片轮换
----------------------------------------------------------------------------------------------------
实现多线程程序的一种方式:从Thread类继承
class MultiThread
{
        public static void main(String[] args)//main()方法也是在一个线程当中被执行的
        {
              MyThread mt=new MyThread();
              //mt.setDaemon(true);//将mt声明为后台线程。public final void setDaemon boolean on):Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.This method must be called before the thread is started.on - if true, marks this thread as a daemon thread.
              mt.setPriority(Thread.MAX_PRIORITY);//设置线程优先级void setPriority(int newPriority):Changes the priority of this thread;static int MAX_PRIORITY:The maximum priority that a thread can have.  
              mt.start();//void start():Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
              int index=0;
              while(true)
              {
                  if(index++==1000)
                      break;
                  System.out.println("main:"+Thread.currentThread().getName());//static Thread currentThread():Returns a reference to the currently executing thread object; String getName():Returns this thread's name.  
              }
        }
}
class MyThread extends Thread//There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread.
{
              public void run()
              {
                   while(true)      
                   {
                        System.out.println(getName());
                         //yield();//中止自己static void yield():Causes the currently executing thread object to temporarily pause and allow other threads to execute.
                   }
              }
}
/*
D:\java\L5>javac MultiThread.java
D:\java\L5>java MultiThread
main:main
Thread-0
在main()里,原本写的是先调用mt.start(),即启动mt线程,再打印main线程,但结果里是先打印出来了main线程。这是因为OS分配给main线程的时间片刚开始还没有用完,所以继续执行打印了main线程,等main线程的时间片执行完了,才执行的MyThread线程
*/
-----------------------------------------------------------------------------------------------------------------
实现多线程程序的另一种方式:实现Runnable接口The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
class MultiThread
{
        public static void main(String[] args)
        {
              MyThread mt=new MyThread();
              new Thread(mt).start();//Thread的一个构造方法:Thread(Runnable target):Allocates a new Thread object.
              int index=0;
              while(true)
              {
                  System.out.println("main:"+Thread.currentThread().getName());
              }
        }
}
class MyThread implements Runnable//这个MyThread已经不是从Thread类派生来的了
{
              public void run()
              {
                   while(true)      
                   {
                        System.out.println(Thread.currentThread().getName());
                   }
              }
}

------------------------------------------------------------------------------------------
   线程的同步
     The code segments within a program that access the same object from separate,conxurrent threads are called "critical sections"
     同步的两种方式:同步快和同步方法。不管是那种方式,都是用synchronized来实现的
     每一个对象都有一个监视器,或者叫做锁。同步方法利用的是this所代表的对象的锁。每个class也有一个锁,是这个class所对应的Class对象的锁
   wait、notify、notifyAll
     每一个对象出了一个锁之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的等待队列时空的
     我们应该在当前线程锁住对象的锁后,去掉用该对象的wait方法
     当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程
     当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程
     wait和notify主要用于生产者—消费者这种关系中
-------------------------------------------------------------------------------------------
火车站售票系统
class TicketsSystem
{
            public static void main(String[] args)
            {
                SellThread st=new SellThread();//同时卖这100张票,不是应该创建4个SellThread对象(SellThread st1=new SellThread()),因为如果是创建4个SellThread对象,那每个对象里都有100张票
                new Thread(st).start();//创建4个线程同时卖这100张票
                new Thread(st).start();
                new Thread(st).start();
                new Thread(st).start();
            }
}
class SellThread implements Runnable
{
     int tickets=100;
     Object obj=new Object();
     public void run()
     {
         while(true)
      {
             synchronized(obj)//可以用synchronized(this)
             {        
                 if(tickets>0)
          {
                     try
                     {
                  Thread.sleep(10);
                     }
              catch(Exception e)
       {
                     e.printStackTrace();
              }
                     System.out.println(Thread.currentThread().getName()+
                                      " sell ticket:"+tickets);
                     tickets--;
                }
        }
            //sell();
      }
}
      public synchronized void sell()
      {
          if(tickets>0)
{
   System.out.println(Thread.currentThread().getName()+
                        " sell ticket:"+tickets);
          tickets--;
         }
       }
}
posted @ 2007-08-18 14:02 jadmin 阅读(72) | 评论 (0)编辑 收藏

  为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程。汉语编程是怎么回事我也不大清楚,应该是前年吧,我一朋友给我介绍汉语编程,怎么不错不错?当时因为学习忙没去关注这个,等我闲了,那个朋友不弄这个,问他他也不说不大清楚,最后自己对这个学习也不了了之了。

今天我写这个不是讲解中英文之间的差距,解码等,我是将我在这几年工作遇到各种各样的乱码的解决方法,总结一样,也希望大家能把自己晕倒解决乱码的方法都说出来,咱们弄一个解决乱码的“葵花宝典”。

  对于Java由于默认的编码方式是 UNICODE,所以用中文也易出问题,常见的解决是
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);

  1、utf8解决JSP中文乱码问题

  一般说来在每个页面的开始处,加入:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%
request.setCharacterEncoding("UTF-8");
%>

  charset=UTF-8 的作用是指定JSP向客户端输出的编码方式为“UTF-8”

  pageEncoding="UTF-8" 为了让JSP引擎能正确地解码含有中文字符的JSP页面,这在LINUX中很有效

  request.setCharacterEncoding("UTF-8"); 是对请求进行了中文编码

  有时,这样仍不能解决问题,还需要这样处理一下:

String msg = request.getParameter("message");
String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");
out.println(st);

  2、Tomcat 5.5 中文乱码

  只要把%TOMCAT安装目录%/ webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下,如果没有filters目录,就创建一个。

  2)在你的web.xml里加入如下几行:

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  3)完成.

  2 get方式的解决办法

  1) 打开tomcat的server.xml文件,找到区块,加入如下一行:

URIEncoding=”GBK”

  完整的应如下:
 
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>

 2)重启tomcat,一切OK。

  3、xmlHttpRequest中文问题

  页面jsp用的GBK编码

<%@ page contentType="text/html; charset=GBK"%>

  javascript部分

function addFracasReport() {
var url="controler?actionId=0_06_03_01&actionFlag=0010";
var urlmsg="&reportId="+fracasReport1.textReportId.value; //故障报告表编号

var xmlHttp=Common.createXMLHttpRequest();
xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage"));
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded);
xmlHttp.send(urlmsg);

}

  后台java中获得的reportId是乱码,不知道该怎么转,主要是不知道xmlHttp.send(urlmsg); 以后是什么编码?在后面用java来转,试了几种,都没有成功,其中有:

public static String UTF_8ToGBK(String str) {
try {
return new String(str.getBytes("UTF-8"), "GBK");
} catch (Exception ex) {
return null;
}
}

public static String UTF8ToGBK(String str) {
try {
return new String(str.getBytes("UTF-16BE"), "GBK");
} catch (Exception ex) {
return null;
}
}

public static String GBK(String str) {
try {
return new String(str.getBytes("GBK"),"GBK");
} catch (Exception ex) {
return null;
}
}
public static String getStr(String str) {
try {
String temp_p = str;
String temp = new String(temp_p.getBytes("ISO8859_1"), "GBK");
temp = sqlStrchop(temp);
return temp;
} catch (Exception e) {
return null;
}
}

  4、JDBC ODBC Bridge的Bug及其解决方法

  在编写一数据库管理程序时,发现JDBC-ODBC Bridge存在不易发现的Bug。在向数据表插入数据时,如果为英文字符,存储内容完全正确,如果存入中文字符,部分数据库只能存储前七八个中文字符,其他内容被截去,导致存储内容的不完整(有些数据库不存在这个问题,如Sybase SQL Anywhere 5.0。JDBC-ODBC Bridge还存在无法建表的Bug)。

  对于广大需要存储中文信息的Java程序员来说,这可是一个不好的消息。要么改用其他语言编程,要么选择其他价格昂贵的数据库产品。“一次编写,到处运行”的目标,也大打折扣。能不能采用变通的方法,将中文信息进行处理后再存储来解决这个问题呢?答案是肯定的。

  解决问题的具体思路、方法

  Java采用Unicode码编码方式,中英文字符均采用16bit存储。既然存储英文信息是正确的,根据一定规则,将中文信息转换成英文信息后存储,自然不会出现截尾现象。读取信息时再进行逆向操作,将英文信息还原成中文信息即可。由GB2312编码规则可知,汉字一般为二个高位为1的ASCII码,在转换时将一个汉字的二个高位1去掉,还原时再将二个高位1加上。为了处理含有英文字符的中文字串,对英文字符则需要加上一个Byte 0标记。以下提供的两个公用静态方法,可加入任何一个类中使用。

  将中英文字串转换成纯英文字串

  public static String toTureAsciiStr(String str){

  StringBuffer sb = new StringBuffer();

  byte[] bt = str.getBytes();

  for(int i =0 ; i〈bt.length; i++){

  if(bt[i]〈0){

  //是汉字去高位1

  sb.append((char)(bt[i]&&0x7f));

   }else{//是英文字符 补0作记录

  sb.append((char)0);

  sb.append((char)bt[i]);

   }

   }

  return sb.toString();

  }

  将经转换的字串还原

  public static String unToTrueAsciiStr(String str){

   byte[] bt = str.getBytes();

   int i,l=0,length = bt.length,j=0;

   for(i = 0; i〈length; i++){

   if(bt[i] == 0){

   l++;

   }

   }

   byte []bt2 = new byte[length-l];

   for(i =0 ; i〈length; i++){

   if(bt[i] == 0){

   i++;

   bt2[j] = bt[i];

   }else{

   bt2[j] = (byte)(bt[i]|0x80);

   }

   j++;

   }

  String tt = new String(bt2);

  return tt;

  }

  上例在实际编程中效果很好,只是存储的中文信息需要经过同样处理,才能被其他系统使用。而且如果中文字串出现英文字符,实际上增加了额外的存储空间。

  5、Solaris下Servlet编程的中文问题及解决办法

  在使用Java开发Internet上的一个应用系统时,发现在Windows下调试完全正常的Servlet,上传到Solaris 服务器上,运行却出现故障——返回的网页不能显示中文,应为中文的信息全为乱码;用中文信息做关键字,不能正确检索数据库。后来采用加入检查代码等方法探知故障原因如下:

  显示乱码主要是因为通过类 HttpServletResponse提供的方法setContentType 无法改变返回给客户的数据的编码方式,正确的编码方式应为GB2312或者GBK,而事实上为缺省的ISO8859-1。无法检索中文信息则是因为,客户提交的中文信息经浏览器编码到达服务器后,Servlet无法将其正确解码。

  举例说明显示乱码解决方法

  Servlet 一般通常做法如下:

  public class ZldTestServlet extends HttpServlet {

  public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{

  //在使用 Writer向浏览器返回数据前,设置 content-type header ,在这里设置相应的字符集gb2312

  response.setContentType("text/html; charset=gb2312");

  PrintWriter out = response.getWriter(); //*

  // 正式返回数据

  out.println("〈html〉〈head〉〈title〉Servlet test〈/title〉〈/head〉" );

  out.println("这是一个测试页!");

  out.println("〈/body〉〈/html〉");

  out.close();

  }

   ...

  }

  解决页面显示乱码问题,需将*处代码换成如下内容:

  PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(),"gb2312"));

  Solaris中文信息检索问题的解决
  浏览器利用表单向服务器提交信息时,一般采用x-www-form-urlencoded 的MIME格式对数据进行编码。如果使用get方法,参数名称和参数值经编码后附加在URL后,在Java中称作查询串(query string)。

  在Servlet程序中,如果采用ServletRequest的方法getParameter取得参数值,在Solaris环境下,对汉字却不能正确解码。因而无法正确检索数据库。

  在Java 1.2的包——java.net中提供了URLEncode和URLDecode类。类URLEncode提供了按x-www-form-urlencoded格式对给定串进行转换的方法。类URLEncode则提供了逆方法。

  6、Common Mail乱码问题

  common mail是一个小而方便的mail包,他实现了对Java Mail的封装,使用起来十分的方便,但是我在使用他的时候发现,使用纯文本的内容发送,结果是乱码,代码如下:

public class TestCommonMail {
public static void main(String[] args) throws EmailException, MessagingException {
SimpleEmail email = new SimpleEmail();
email.setCharset("GB2312");
email.setHostName("smtp.163.com");
email.setSubject("test");
email.addTo("test@163.com");
email.setFrom("test@163.com");
email.setMsg("我的测试");
email.setAuthentication("test", "test");
email.send();
}
}

分析了一下commons mail的源码找到了原因。源码如下:

public class SimpleEmail extends Email
{
public Email setMsg(String msg) throws EmailException, MessagingException
{
if (EmailUtils.isEmpty(msg))
{
throw new EmailException("Invalid message supplied");
}

setContent(msg, TEXT_PLAIN);
return this;
}
}

Email代码片段

public void setContent(Object aObject, String aContentType)
{
this.content = aObject;
if (EmailUtils.isEmpty(aContentType))
{
this.contentType = null;
}
else
{
// set the content type
this.contentType = aContentType;

// set the charset if the input was properly formed
String strMarker = "; charset=";
int charsetPos = aContentType.toLowerCase().indexOf(strMarker);
if (charsetPos != -1)
{
// find the next space (after the marker)
charsetPos += strMarker.length();
int intCharsetEnd =
aContentType.toLowerCase().indexOf(" ", charsetPos);

if (intCharsetEnd != -1)
{
this.charset =
aContentType.substring(charsetPos, intCharsetEnd);
}
else
{
this.charset = aContentType.substring(charsetPos);
}
}
}
}

email.send(); 的send方法将调用
public void buildMimeMessage() throws EmailException
{
try
{
this.getMailSession();
this.message = new MimeMessage(this.session);

if (EmailUtils.isNotEmpty(this.subject))
{
if (EmailUtils.isNotEmpty(this.charset))
{
this.message.setSubject(this.subject, this.charset);
}
else
{
this.message.setSubject(this.subject);
}
}

// ========================================================
// Start of replacement code
if (this.content != null)
{
this.message.setContent(this.content, this.contentType);
}
// end of replacement code
// ========================================================
else if (this.emailBody != null)
{
this.message.setContent(this.emailBody);
}
else
{
this.message.setContent("", Email.TEXT_PLAIN);
}

if (this.fromAddress != null)
{
this.message.setFrom(this.fromAddress);
}
else
{
throw new EmailException("Sender address required");
}

if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)
{
throw new EmailException(
"At least one receiver address required");
}

if (this.toList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.TO,
this.toInternetAddressArray(this.toList));
}

if (this.ccList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.CC,
this.toInternetAddressArray(this.ccList));
}

if (this.bccList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.BCC,
this.toInternetAddressArray(this.bccList));
}

if (this.replyList.size() > 0)
{
this.message.setReplyTo(
this.toInternetAddressArray(this.replyList));
}

if (this.headers.size() > 0)
{
Iterator iterHeaderKeys = this.headers.keySet().iterator();
while (iterHeaderKeys.hasNext())
{
String name = (String) iterHeaderKeys.next();
String value = (String) headers.get(name);
this.message.addHeader(name, value);
}
}

if (this.message.getSentDate() == null)
{
this.message.setSentDate(getSentDate());
}

if (this.popBeforeSmtp)
{
Store store = session.getStore("pop3");
store.connect(this.popHost, this.popUsername, this.popPassword);
}
}
catch (MessagingException me)
{
throw new EmailException(me);
}
}
由代码可以知道纯文本方式最终调用了Java Mail的
message.setContent(this.content, this.contentType);
content是内容
contentType是类型,如text/plain,
(我们可以试试直接用Java mail发邮件,设置文本内容不使用setText方法,也使用setContent("测试", "text/plain")方式,你可以看到内容也是乱码)

  关键就在于text/plain,我们改成text/plain; charset=gb2312,ok乱码解决了。在commons mail我们看SimpleEmail 类中setMsg方法调用的就是 setContent(msg, TEXT_PLAIN); 我们只需要将Email类中的常量TEXT_PLAIN修改一下加入 charset=你的字符集 ,重新打包jar,这样就可以了

  7、toad的字符集的设置与oracle的安装

  oracle数据库服务器的安装一般是中文字符集,有时安装在不同的平台下,设置为ISO编码,toad是oracle开发的最好工具,不是我说的,可是中文环境下安装的toad,打开英文字符的oracle时,中文全是乱码。必须进行设置

环境变量---〉系统变量

NLS_lANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK

NLS_lANG=AMERICAN_AMERICA.WE8ISO8859P1

AMERICAN_AMERICA.WE8MSWIN1252

或者

打开注册表,点击HKEY_LOCAL_MATHINE
再点击Software,再点击ORACLE
在点击HOME(ORACLE所在目录)
在注册表的右半面有NLS_LANG,
双击它,将你想要的覆盖掉原来的就可以了
最好记下旧的,以便可以改回来。

connect sys/chang_on_install
update props$
set value$='ZHS16CGB231280'
where name='NLS_CHARACTERSET';
commit;
这样就OK了

  8、如何解決GWT(google web toolkit)中文的問題

  GWT 中文乱码解决方法

1.把你要显示的中文“测试字符串”输入到一个文件,如:1.txt
2.进入命令行,进入1.txt所在的目录,敲入以下命令:native2ascii.exe 1.txt 2.txt 回车。这样就生成了另外一个文件2.txt。
3.2.txt的内容如下:\u6d4b\u8bd5\u5b57\u7b26\u4e32
4.然后用上面的编码,在gwt中使用,就可以了.

  9、xmlHttp得到的网页怎么是乱码?

  (1)在服务器端使用WebRequest而不是xmlHttp
  (2) 将

StreamReader sr = new StreamReader(stream);

  对于简体中文改成:

StreamReader sr = new StreamReader(stream , Encoding.Default );

  对于utf-8改成:

StreamReader sr = new StreamReader(stream , Encoding.UTF8 );

  当然,Encoding枚举还有很多其他的成员,对于不同的编码content-type可以有选择的应用

  (3)后来我发现无论是content-type是gb2312还是utf-8,用

StreamReader sr = new StreamReader(stream , Encoding.Default );

  都可以返回正常的汉字,所以统一的改成Encoding.Default




--------------------------------------------------------------------------------

最后,在服务器端从一个url获得网页的源代码的代码如下:



/// <summary>
/// post一个指定的url,获得网页的源代码(用WebRequest实现)
/// </summary>
/// <param name="url"></param>
/// <returns>
/// 如果请求失败,返回null
/// 如果请求成功,返回网页的源代码
/// </returns>
public static string GetContentFromUrl2( string url )
{
//变量定义
string respstr;

WebRequest myWebRequest=WebRequest.Create(url);
// myWebRequest.PreAuthenticate=true;
// NetworkCredential networkCredential=new NetworkCredential( username , password , domain );
// myWebRequest.Credentials=networkCredential;

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();
System.IO.Stream stream = myWebResponse.GetResponseStream();
StreamReader sr = new StreamReader(stream , Encoding.Default );
//以字符串形式读取数据流
respstr = sr.ReadToEnd();
sr.Close();

return respstr;

}

来源:soddabao-BlogJava

posted @ 2007-08-17 02:00 jadmin 阅读(47) | 评论 (0)编辑 收藏

  前言

  ant是java开发者工具箱的重要一环,junit,xdoclet等都与它紧密关联,程序员可能习惯了IDE提供的自动构建,甚至部署的功能,从而忽略了ant本身,其实,主流的IDE通常是内置ant任务来完成这些工作的,熟悉ant内在的机理,可以阅读或简单修改build.xml无疑可以帮助你更灵活地集成、管理应用项目,如果需要学习maven这种开源项目管理解决方案,也是要以理解ant为基础的哟。另外,使用ant的过程实际上对构建进行了文档化,它是无关于IDE的,想象一下,你的同事中可能三分之一在用JbuilderX,三分之一用eclipse,还有一些是别的。

  本人使用eclipse3.0.1,以前的构建和发布工作都由myeclipse插件作了,趁周末实践了一下手动构建,记此备忘。

  实践

  准备工作:这是我的个人习惯,把所有公用的类库jar置于一个固定目录,分好类,不要丢在一个文件夹下,如jakarta-commons、hibernate、spring、struts等,这些是源码构建时需要用到的,在部署时可能有一些不用再打进去了,比如servlet.jar。如果你们有自己的framework,也一并放在这里。然后,打开eclipse,进入Windows->Preferences->Java->User Libraries,增加一个自己的库,比如说mylib,把刚才那些公共的jar全部添入,这样有个好处,在eclipse项目中,不用再看到烦人的长长的jar列表了,比较整洁。


  下来正式进行:

  1.新建一个Java Project,此时就不要再选你的j2ee插件内置的一些选项了,至简即可。

  2.在root下建几个文件夹,我们在网上下载的开源项目中经常可以看到这些,比如:

  src - 源码
  classes - 编译
  web - jsp等
  lib - 库,这里可以简单地把mylib下的东东copy过来,便于将来发布源码。
  dlist - 输出的jar或war

  当然,我们要建一个build.xml,eclipse中会出现一个蚂蚁的小图标,一般这个文件建立后,下一个项目简单的copy过去,稍加改动就可以了。

  3.打开项目的属性页,在Java Build Path的库选项中,加入我们自定义的公共库mylib.至于Builders方式就不用改了,使用默认的Java Builer即可,我只是项目部署时使用ant,平常的排错工作就交给IDE吧。

  4.重中之重,写你的build.xml,网上文章很海,我这里就不再?嗦了,基本上就分那几个任务:

  4.1 先要声明一些路径变量,如

  <property name="war.dir" value="dlist" />

  也可以将其写至properties文件中,在这里引用;

  4.2 声明编译的类路径,如下:

  <path id="master-classpath">
  <fileset dir="${lib.root}/struts">
  <include name="struts-menu-2.3.jar" />
  <include name="struts.jar" />
  </fileset>
  <fileset dir="${lib.root}/jakarta-commons">
  <include name="commons-*.jar" />
  </fileset>
  <fileset dir="${lib.root}/ibatis2.0.9">
  <include name="ibatis-*.jar" />
  </fileset>
  <fileset dir="${lib.root}/jdbcdriver">
  <include name="jtds-0.9-rc2.jar" />
  </fileset>s
  ......
  </path>

  4.3 清空输出目录,如web,dlist等。

  4.4 编译构建:

  <target name="build" description="Compile main source tree java files into class files, generate jar files">

  <mkdir dir="${build.dir}" />

  <javac destdir="${build.dir}" source="1.3" target="1.3" debug="true" deprecation="false" optimize="false" failonerror="true">
  <src path="${src.dir}" />
  <classpath refid="master-classpath" />
  </javac>

  <copy todir="${build.dir}" preservelastmodified="true">
  <fileset dir="${src.dir}">
  <include name="**/*.xml" />
  <include name="**/*.properties" />
  </fileset>
  </copy>
  <!-- ============================================= -->
  <!-- 据测试,资源文件不能被打到jar文件中,其余均可 -->
  <!-- ============================================= -->
  <copy todir="${webclasses.dir}/conf" preservelastmodified="true">
  <fileset dir="${src.dir}/conf">
  <include name="springResources*.properties" />
  </fileset>
  </copy>

  <mkdir dir="${weblib.dir}" />

  <jar jarfile="${weblib.dir}/${name}.jar" compress="true">
  <fileset dir="${build.dir}">
  <include name="**" />
  </fileset>
  </jar>

  <copy todir="${weblib.dir}" preservelastmodified="true">

  <fileset dir="${lib.root}">
  <include name="log4j-1.2.8.jar" />
  </fileset>
  <fileset dir="${lib.root}/struts">
  <include name="struts-menu-2.3.jar" />
  <include name="struts.jar" />
  </fileset>
  <fileset dir="${lib.root}/jakarta-commons">
  <include name="commons-*.jar" />
  </fileset>
  <fileset dir="${lib.root}/spring-1.1.3">
  <include name="spring.jar" />
  <include name="aopalliance.jar" />
  </fileset>
  ......

  </copy>

  </target>

  <!-- ============================================= -->
  <!-- Compile main Java sources and copy libraries -->
  <!-- ============================================= -->
  <target name="warfile" description="Build the web application archive">

  <mkdir dir="${dist.dir}" />
  <war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml">
  <include name="*" />
  <include name="WEB-INF/*.*" />
  <exclude name="WEB-INF/web.xml" />
  <include name="WEB-INF/classes/*.*" />
  <include name="WEB-INF/lib/**" />
  <exclude name="**/.*" />
  </war>

  </target>


  4.5 打成war

  <target name="warfile" description="Build the web application archive">

  <mkdir dir="${dist.dir}" />
  <war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml">
  <include name="*" />
  <include name="WEB-INF/*.*" />
  <exclude name="WEB-INF/web.xml" />
  <include name="WEB-INF/classes/*.*" />
  <include name="WEB-INF/lib/**" />
  <exclude name="**/.*" />
  </war>

  </target>

  4.6 把几个任务串起来,弄一个default target

  <target name="all">
  <antcall target="clean" />
  <antcall target="build" />
  <antcall target="warfile" />
  </target>

  打完收功。在实践中发现,一些配置文件,如struts-config.xml ibatis和spring的xml都可以打进jar文件,spring资源文件好象不行,得单独copy至WEB-INFclasses下,另外,你的web文件夹下,事先得放好web.xml,以及一些tld文件哟!

posted @ 2007-08-15 01:33 jadmin 阅读(69) | 评论 (0)编辑 收藏
1。安装tomcat5.5的admin package tomcat5.5版本中的admin没有默认安装,先下载admin package jakarta-tomcat-5.5.9-admin.zip或者jakarta-tomcat-5.5.9-admin.tar.gz(这两个package应该是一样的,下载地址http://mirrors.uol.com.br/pub/apache/jakarta/tomcat-5/v5.5.9/bin/)。
  
  将其中的/conf/Catalina/localhost/admin.xml复制到%JAVA_HOME%\conf\Catalina\localhost\目录下,将server/webapps/admin文件夹复制到%JAVA_HOME%\server\webapps目录下。另外的RELEASE-NOTES,NOTICE,LICENSE直接复制到%JAVA_HOME%目录下即可,不知道是不是一定得要的,自己可以试试,呵呵。这样就完成了admin package的安装。
  
  2。配置admin 上面完成了admin package的安装,现在可以进入http://localhost:8080/admin。首先进入一个登陆窗口,用户名默认是admin,密码为空,当然你可以在安装tomcat的时候修改。进入以后,找到Data Sources,单击,在右边的data Source Actions中选择Create New Data Source.下面是要填写的信息。
  
  (示例) 如果你安装的是ms的jdbc;
  
  JNDI Name:jdbc/sqlserver Data Source URL:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sqlserver JDBC Driver Class: com.microsoft.jdbc.sqlserver.SQLServerDriver 驱动名称 User Name:sa//数据库登陆用户名和密码 Password:sa Max.Active Connections:4 Max.Idle Connections:2 Max.Wait for Connection:5000 Validation Query: 如果你安装的是jtds; JNDI Name:jdbc/sqlserver Data Source URL:jdbc:jtds:sqlserver://localhost:1433/sqlserver 其中sqlserver是你的数据库名称 JDBC Driver Class: net.sourceforge.jtds.jdbc.Driver 驱动名称 User Name:sa//数据库登陆用户名和密码 Password:sa Max.Active Connections:4 Max.Idle Connections:2 Max.Wait for Connection:5000 Validation Query:
字串5

  
  3。文件配置通过文件夹导航到%TOMCAT_HOME%\conf,打开web.xml,在的前面添加以下内容: DB Connection jdbc/sqlserver javax.sql.DataSource Container
  
  注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。 配置完成后重新启动tomcat,在%JAVA_HOME%\\webapps\ROOT\META-INF下找到对应的配置文件Context.xml,检查其内容。
  
  下面是参考信息。
  
  WEB-INF/web.xml WEB-INF/web.xml 如果是旧版本,那么通过文件夹导航到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web应用对应的.xml文件,如xmdc.xml,并在此文件之前添入一行代码:
  
  重新启动tomcat,应该没什么问题了。
posted @ 2007-08-15 01:24 jadmin 阅读(42) | 评论 (0)编辑 收藏
仅列出标题
共50页: First 上一页 31 32 33 34 35 36 37 38 39 下一页 Last