2012年2月29日星期三

httpClient应用 - tomrose - ITeye技术网站

httpClient应用 - tomrose - ITeye技术网站

aop在接口测试中的应用 » Taobao QA Team

aop在接口测试中的应用 » Taobao QA Team

Hudson扩展之插件开发 » Taobao QA Team

Hudson扩展之插件开发 » Taobao QA Team

hudson插件开发入门(一) - 雨中漫步 - 51CTO技术博客

hudson插件开发入门(一) - 雨中漫步 - 51CTO技术博客

hudson插件开发糊涂虫o(︶︿︶)o清醒时分_百度空间

hudson插件开发糊涂虫o(︶︿︶)o清醒时分_百度空间

Java中用XML Schema检验XML实战 - Michael Ma的博客 - ITeye技术网站

Java中用XML Schema检验XML实战 - Michael Ma的博客 - ITeye技术网站


上一篇文章"XML Schema检验XML文档结构"中,简单介绍了XML Schema及其写法,这篇主要针对java开发者,在程序中检验XML的有效性。

由于程序中经常会对xml的validation进行检验,通过DOM解析再一项一项check的方式,陈旧而且写起来繁杂。本文介绍一种快速检验 xml的方法,借助javax.xml.validation包下的Validator, Schema, SchemaFactory等类,通过传入XML Schema Definition(XSD)文件进行验证。

代码如下:

public static void main(String[] args) throws IOException, SAXException {   String xmlFile = "test.xml"  String xsdFile = "test.xsd"   // 1. Lookup a factory for the W3C XML Schema language  SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");   // 2. Compile the schema.   File schemaLocation = new File(xsdFile);  Schema schema = factory.newSchema(schemaLocation);   // 3. Get a validator from the schema.  Validator validator = schema.newValidator();   // 4. Parse the document you want to check.  Source source = new StreamSource(xmlFile);   // 5. Check the document  try {   validator.validate(source);   System.out.println(xmlFile  + " is valid.");   } catch (SAXException ex) {   System.out.println(xmlFile  + " is not valid because ");   System.out.println(ex.getMessage());  } }

2012年2月25日星期六

Re: 研究一下怎么利用业余时间网上赚点儿钱?(转寄)

发信人: coppoia (merlot), 信区: StartUp
标 题: Re: 研究一下怎么利用业余时间网上赚点儿钱?
发信站: BBS 未名空间站 (Sat Feb 25 11:59:15 2012, 美东)

国内好多小站长。 都是建30-50个这样的小网站赚钱。你找找他们在国内的论坛。应该
可以学到好多经验。 不过靠google赚这钱有点靠天吃饭的味道。 怎么都不是很长久
的事。

另外市场调查还是很重要的。 至少要知道你的竞争对手是哪些。 还有没有生存空间。
机票/旅馆这类大网站已经有多少了。 这些是越大越有价格上的竞争力的。 人家根本
就是卖批发价。 搜索引擎也是专业的专业。

可以做的小网站有很多。 例如现在国内有钱人那么多。 共产党的局势又从来没稳定过
。 尤其对那些贪了赚了的。 你建个介绍美国绿卡投资的网站。 一步一步简单详细的
程序。 他们想看明白他们能不能做绿卡投资,至少要在你的网站上耗几个小时。 他
们在你网站的时间就是金钱啦。

【 在 Uwin (Uwin) 的大作中提到: 】
: 这个话题实际上存在好多年了,但前几天看到本版有个哥们买了个网站,问大家的意见
: ,然后我好信儿也去了那个买卖网站的地方买了2个,想利用google adsense呀,
: affiliate program呀什么的赚点儿小钱,自己也挺喜欢搞网页设计的,就算玩吧。等
: 进入了这个领域才发现有太多的学问了,什么SEO优化呀,自动在各大网站发文章,自
: 动加入无数个facebook, google+1好友什么的,已经学习了一个多礼拜了,还是没什
: 么成效。
: 这是我弄的几个网站,大家可以从这里开始探讨怎么利用类似的网站赚钱,或敲门儿,
: 1。 www.airlineticketsdeals.org 一个搜索旅馆和机票的地方,原来想即使不赚钱,
: 至少自己可以利用这个搜索便宜的deal,不知道各位童鞋觉得这个通吗?
: 2。 www.iUnlockPlus.com 顾名思义,是unlock/jailbreak iphone的服务,有中国哥
: ...................

--

※ 来源:・WWW 未名空间站 海外: mitbbs.com 中国: mitbbs.cn・[FROM: 71.174.]

Re: 研究一下怎么利用业余时间网上赚点儿钱?(转寄)

发信人: Uwin (Uwin), 信区: StartUp
标 题: Re: 研究一下怎么利用业余时间网上赚点儿钱?
发信站: BBS 未名空间站 (Sat Feb 25 11:52:57 2012, 美东)

我用的是苹果电脑,有个软件叫rapidweaver,比苹果本身的iweb好用多了,我又下载
了个好看的模板,所以做出来就那样了,我自己也喜欢设计,所以正好。哪些朋友想做
点儿啥,我可以帮忙,比较快。可以给我留言。

回 drmj and dengfu, 对呀,这些都是靠人家吃饭,所以钱不好整,但大多是也都是自
动的,自己不用update太多,还好了。你们说道正点儿上了,流量是个大问题,不过有
一些捷径可以走,所以正在研究,像Senuke软件好像就能承担一部分这样的事情。

那个买卖网站叫 flippa.com 但一定小心,上面基本上都是吹牛的,而且有些卖家,换
个domain名,内容一样,又继续卖,不好。

paypal 上注册比较简单,不用business名称,你自己的名字就行。


--

※ 来源:・WWW 未名空间站 海外: mitbbs.com 中国: mitbbs.cn・[FROM: 75.73.]

研究一下怎么利用业余时间网上赚点儿钱?(转寄)

发信人: Uwin (Uwin), 信区: StartUp
标 题: 研究一下怎么利用业余时间网上赚点儿钱?
关键字: 网站设计,affiliate, amazon,旅馆机票,网站流量
发信站: BBS 未名空间站 (Sat Feb 25 01:08:06 2012, 美东)

这个话题实际上存在好多年了,但前几天看到本版有个哥们买了个网站,问大家的意见
,然后我好信儿也去了那个买卖网站的地方买了2个,想利用google adsense呀,
affiliate program呀什么的赚点儿小钱,自己也挺喜欢搞网页设计的,就算玩吧。等
进入了这个领域才发现有太多的学问了,什么SEO优化呀,自动在各大网站发文章,自
动加入无数个facebook, google+1好友什么的,已经学习了一个多礼拜了,还是没什
么成效。
这是我弄的几个网站,大家可以从这里开始探讨怎么利用类似的网站赚钱,或敲门儿,

1。 www.airlineticketsdeals.org 一个搜索旅馆和机票的地方,原来想即使不赚钱,
至少自己可以利用这个搜索便宜的deal,不知道各位童鞋觉得这个通吗?

2。 www.iUnlockPlus.com 顾名思义,是unlock/jailbreak iphone的服务,有中国哥
们想unlock iphone,站内告诉我,我免费把方法和文件发给你们。如果老外访问的话
,就自己交钱吧。实际上要是多费点儿功夫,网上可以找到免费的方法,我只是想买一
个这个网站看看。

3。 通过上面这两个学习过程中,发现amazon有affiliate,就是你自己随便做一个网
站,把amazon的e-store放在上面,如果有人从你这里转到amazon买东西,你就可以赚
些回扣了,以后自己通过自己的这个网站买,也能有点儿回扣。这是我做的网址 www.
GoodForYouInc.com

4. 就是怎么提高网站流量呢,这是大问题,我正在研究中,希望大家交流。这个也是
一个网站 www.iPromoteDeal.com,就是一些方法,所以这些服务你都可以outsourcing
,还比较便宜。

大家讨论讨论吧,我是一周前看了那个哥们的帖子才开始这些的,不值时间是不是白浪
费了,这一周着迷搞这个了,被老婆骂好几顿了,嘻嘻。
--

※ 来源:・WWW 未名空间站 海外: mitbbs.com 中国: mitbbs.cn・[FROM: 75.73.]

八阕 · 广角新闻 · 生 活:【日本人写的中国饮食安全攻略 让国人看了汗颜(组图)】

八阕 · 广角新闻 · 生 活:【日本人写的中国饮食安全攻略 让国人看了汗颜(组图)】

2012年2月24日星期五

WTP Tutorials - JavaServer Faces Tools Tutorial

WTP Tutorials - JavaServer Faces Tools Tutorial

Make the Web Page Editor the default editor for JSP pages

The JSF Tools Project-contributed Web Page Editor (WPE) is NOT registered as the default editor for JSP files. You can make the WPE the default editor for JSP files by following these steps:
  • Navigate to "Window" -> "Preferences..." -> "General" -> "Editors" -> "File Associations"
  • In the "File types" section, select "*.jsp"
  • In the "Associated editors" section, select "Web Page Editor"
  • Select the "Default" button

WTP Tutorials - JavaServer Faces Tools Tutorial

WTP Tutorials - JavaServer Faces Tools Tutorial

Make the Web Page Editor the default editor for JSP pages

The JSF Tools Project-contributed Web Page Editor (WPE) is NOT registered as the default editor for JSP files. You can make the WPE the default editor for JSP files by following these steps:
  • Navigate to "Window" -> "Preferences..." -> "General" -> "Editors" -> "File Associations"
  • In the "File types" section, select "*.jsp"
  • In the "Associated editors" section, select "Web Page Editor"
  • Select the "Default" button

Eclipse plugin for HTML WYSIWYG editor - Stack Overflow

Eclipse plugin for HTML WYSIWYG editor - Stack Overflow


Since this is currently (2011-10-25) at 2nd place in Google for a "Eclipse HTML WYSIWYG" query and the question linked by Rafa de Castro dont give a complete answer, I feel that this question needs an update.


For now, the only available WYSIWYG tools for HTML I found are:

  • WTP JavaServer Faces contains a tool named Web Page Editor. Nice, free and also serves as a visual editor.
  • JBoss Tools HTML Editor is also free, neat, has two preview modes, but provides only simplest visual editing functionality.
  • Eclipse Visual Designer shipped with MyEclipse. It can even preview pages in IE/Firefox, but it has a huge disadvantage - it's a payed software. Looks awesome, but I didnt try it, so I can't say anything about quality.

A note about intsallation.

Installing something into Eclipse is sometimes a pain, especially for a new user.

So, if you want to install just an HTML editor from WTP or JBoss without installing the whole thing with it, you can do the following (steps are vadid at least for Eclipse 3.6 and 3.7.):

  • Open Eclipse, select Help / Install New Software.
  • In the Work with: field input the update site address.

For JBoss Tools:

  • Get the update site URL for the appropirate Eclipse version from here
  • Select JBoss Tool RichFaces.

For Web Pages Editor

  • Use an already existing update site, that look like http://download.eclipse.org/releases/
  • Select Web Pages Editor.

Now press Next and prepare to wait for eternity. (You can speed up this process by disabling Contact all update sites during install checkbox)

2012年2月23日星期四

javaMail详细解析(转)_亮仔小空间_百度空间

javaMail详细解析(转)_亮仔小空间_百度空间

Example:

public void sendMail(String type) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "false");

Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);

//setup addresses
message.setRecipients(MimeMessage.RecipientType.TO, to); // multiple addresses in "to" must seperated by ","
if(cc!=null){message.setRecipients(MimeMessage.RecipientType.CC, cc);}
if(bcc!=null){message.setRecipients(MimeMessage.RecipientType.BCC, bcc);}

//setup subject, body and attachment
message.setSubject(subject);

Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
if("html".equals(type)){
mbp.setContent(body, "text/html");
}else{
mbp.setText(body);
}
mp.addBodyPart(mbp);

for (String attachment : attachmentfilenames) {
mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(attachment);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
message.setContent(mp);

//send message
message.saveChanges(); // implicit with send()

Transport.send(message);
}


版权声明:本文可以自由转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig)
原文:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
关键字:java,mail,pop,smtp

一、JavaMail API简介
JavaMail API是读取、撰写、发送电子信息的可选包。我们可用它来建立如Eudora、Foxmail、MS Outlook Express一般的邮件用户代理程序(Mail User Agent,简称MUA)。而不是像sendmail或者其它的邮件传输代理(Mail Transfer Agent,简称MTA)程序那样可以传送、递送、转发邮件。从另外一个角度来看,我们这些电子邮件用户日常用MUA程序来读写邮件,而MUA依赖着 MTA处理邮件的递送。
在清楚了到MUA与MTA之间的关系后,让我们看看JavaMail API是如何提供信息访问功能的吧!JavaMail API被设计用于以不依赖协议的方式去发送和接收电子信息,这个API被分为两大部分:

基本功能:如何以不依赖于协议的方式发送接收电子信息,这也是本文所要描述的,不过在下文中,大家将看到这只是一厢情愿而已。
第二个部分则是依赖特定协议的,比如SMTP、POP、IMAP、NNTP协议。在这部分的JavaMail API是为了和服务器通讯,并不在本文的内容中。

二、相关协议一览
在我们步入JavaMail API之前,先看一下API所涉及的协议。以下便是大家日常所知、所乐于使用的4大信息传输协议:
SMTP
POP
IMAP
MIME
当然,上面的4个协议,并不是全部,还有NNTP和其它一些协议可用于传输信息,但是由于不常用到,所以本文便不提及了。理解这4个基本的协议有助于我们 更好的使用JavaMail API。然而JavaMail API是被设计为与协议无关的,目前我们并不能克服这些协议的束缚。确切的说,如果我们使用的功能并不被我们选择的协议支持,那么JavaMail API并不可能如魔术师一样神奇的赋予我们这种能力。

1.SMTP
简单邮件传输协议定义了递送邮件的机制。在下文中,我们将使用基于Java-Mail的程序与公司或者ISP的SMTP服务器进行通讯。这个SMTP服务 器将邮件转发到接收者的SMTP服务器,直至最后被接收者通过POP或者IMAP协议获取。这并不需要SMTP服务器使用支持授权的邮件转发,但是却的确 要注意SMTP服务器的正确设置(SMTP服务器的设置与JavaMail API无关)。

2.POP
POP是一种邮局协议,目前为第3个版本,即众所周知的POP3。POP定义了一种用户如何获得邮件的机制。它规定了每个用户使用一个单独的邮箱。大多数 人在使用POP时所熟悉的功能并非都被支持,例如查看邮箱中的新邮件数量。而这个功能是微软的Outlook内建的,那么就说明微软Outlook之类的 邮件客户端软件是通过查询最近收到的邮件来计算新邮件的数量来实现前面所说的功能。因此在我们使用JavaMail API时需要注意,当需要获得如前面所讲的新邮件数量之类的信息时,我们不得不自己进行计算。

3.IMAP
IMAP使用在接收信息的高级协议,目前版本为第4版,所以也被称为IMAP4。需要注意的是在使用IMAP时,邮件服务器必须支持该协议。从这个方面 讲,我们并不能完全使用IMAP来替代POP,不能期待IMAP在任何地方都被支持。假如邮件服务器支持IMAP,那么我们的邮件程序将能够具有以下被 IMAP所支持的特性:每个用户在服务器上可具有多个目录,这些目录能在多个用户之间共享。
其与POP相比高级之处显而易见,但是在尝试采取IMAP时,我们认识到它并不是十分完美的:由于IMAP需要从其它服务器上接收新信息,将这些信息递送 给用户,维护每个用户的多个目录,这都为邮件服务器带来了高负载。并且IMAP与POP的一个不同之处是POP用户在接收邮件时将从邮件服务器上下载邮 件,而IMAP允许用户直接访问邮件目录,所以在邮件服务器进行备份作业时,由于每个长期使用此邮件系统的用户所用的邮件目录会占有很大的空间,这将直接 导致邮件服务器上磁盘空间暴涨。

4.MIME
MIME并不是用于传送邮件的协议,它作为多用途邮件的扩展定义了邮件内容的格式:信息格式、附件格式等等。一些RFC标准都涉及了MIME:RFC 822, RFC 2045, RFC 2046, RFC 2047,有兴趣的Matrixer可以阅读一下。而作为JavaMail API的开发者,我们并不需关心这些格式定义,但是这些格式被用在了程序中。

5.NNTP和其它的第三方协议
正因为JavaMail API在设计时考虑到与第三方协议实现提供商之间的分离,故我们可以很容易的添加一些第三方协议。SUN维护着一个第三方协议实现提供商的列表:http://java.sun.com/products/javamail/Third_Party.html,通过此列表我们可以找到所需要的而又不被SUN提供支持的第三方协议:比如NNTP这个新闻组协议和S/MIME这个安全的MIME协议。

三、安装
1.安装JavaMail
为了使用JavaMail API,需要从http://java.sun.com/products/javamail/downloads/index.html下载文件名格式为javamail-[version].zip的文件(这个文件中包括了JavaMail实现),并将其中的mail.jar文件添加到CLASSPATH中。这个实现提供了对SMTP、IMAP4、POP3的支持。
注意:在安装JavaMail实现之后,我们将在demo目录中发现许多有趣的简单实例程序。
在安装了JavaMail之后,我们还需要安装JavaBeans Activation Framework,因为这个框架是JavaMail API所需要的。如果我们使用J2EE的话,那么我们并无需单独下载JavaMail,因为它存在于J2EE.jar中,只需将J2EE.jar加入到 CLASSPATH即可。

2.安装JavaBeans Activation Framework
http://java.sun.com/products/javabeans/glasgow/jaf.html下 载JavaBeans Activation Framework,并将其添加到CLASSPATH中。此框架增加了对任何数据块的分类、以及对它们的处理的特性。这些特性是JavaMail API需要的。虽然听起来这些特性非常模糊,但是它对于我们的JavaMail API来说只是提供了基本的MIME类型支持。
到此为止,我们应当把mail.jar和activation.jar都添加到了CLASSPATH中。
当然如果从方便的角度讲,直接把这两个Jar文件复制到JRE目录的lib/ext目录中也可以。

四、初次认识JavaMail API
1.了解我们的JavaMail环境
A.纵览JavaMail核心类结构
打开JavaMail.jar文件,我们将发现在javax.mail的包下面存在着一些核心类:Session、Message、Address、 Authenticator、Transport、Store、Folder。而且在javax.mail.internet包中还有一些常用的子类。
B.Session
Session类定义了基本的邮件会话。就像Http会话那样,我们进行收发邮件的工作都是基于这个会话的。Session对象利用了java.util.Properties对象获得了邮件服务器、用户名、密码信息和整个应用程序都要使用到的共享信息。
Session类的构造方法是私有的,所以我们可以使用Session类提供的getDefaultInstance()这个静态工厂方法获得一个默认的Session对象:
Properties props = new Properties();
// fill props with any information
Session session = Session.getDefaultInstance(props, null);

或者使用getInstance()这个静态工厂方法获得自定义的Session:
Properties props = new Properties();
// fill props with any information
Session session = Session.getInstance(props, null);

从上面的两个例子中不难发现,getDefaultInstance()和getInstance()方法的第二个参数都是null,这是因为在上面的例子中并没有使用到邮件授权,下文中将对授权进行详细介绍。
从很多的实例看,在对mail server进行访问的过程中使用共享的Session是足够的,即使是工作在多个用户邮箱的模式下也不例外。

C.Message
当我们建立了Session对象后,便可以被发送的构造信息体了。在这里SUN提供了Message类型来帮助开发者完成这项工作。由于Message是 一个抽象类,大多数情况下,我们使用javax.mail.internet.MimeMessage这个子类,该类是使用MIME类型、MIME信息头 的邮箱信息。信息头只能使用US-ASCII字符,而非ASCII字符将通过编码转换为ASCII的方式使用。
为了建立一个MimeMessage对象,我们必须将Session对象作为MimeMessage构造方法的参数传入:
MimeMessage message = new MimeMessage(session);

注意:对于MimeMessage类来讲存在着多种构造方法,比如使用输入流作为参数的构造方法。

在建立了MimeMessage对象后,我们需要设置它的各个part,对于MimeMessage类来说,这些part就是MimePart接口。最基本的设置信息内容的方法就是通过表示信息内容和米么类型的参数调用setContent()方法:
message.setContent("Hello", "text/plain");

然而,如果我们所使用的MimeMessage中信息内容是文本的话,我们便可以直接使用setText()方法来方便的设置文本内容。
message.setText("Hello");

前面所讲的两种方法,对于文本信息,后者更为合适。而对于其它的一些信息类型,比如HTML信息,则要使用前者。
别忘记了,使用setSubject()方法对邮件设置邮件主题:
message.setSubject("First");


D.Address
到这里,我们已经建立了Session和Message,下面将介绍如何使用邮件地址类:Address。像Message一样,Address类也是一个抽象类,所以我们将使用javax.mail.internet.InternetAddress这个子类。
通过传入代表邮件地址的字符串,我们可以建立一个邮件地址类:
Address address = new InternetAddress("president@whitehouse.gov");

如果要在邮件地址后面增加名字的话,可以通过传递两个参数:代表邮件地址和名字的字符串来建立一个具有邮件地址和名字的邮件地址类:
Address address = new InternetAddress("president@whitehouse.gov", "George Bush");

本文在这里所讲的邮件地址类是为了设置邮件信息的发信人和收信人而准备的,在建立了邮件地址类后,我们通过message的setFrom()和setReplyTo()两种方法设置邮件的发信人:
message.setFrom(address);
message.setReplyTo(address);

若在邮件中存在多个发信人地址,我们可用addForm()方法增加发信人:
Address address[] = ...;
message.addFrom(address);

为了设置收信人,我们使用addRecipient()方法增加收信人,此方法需要使用Message.RecipientType的常量来区分收信人的类型:
message.addRecipient(type, address)

下面是Message.RecipientType的三个常量:
Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC
因此,如果我们要发送邮件给总统,并发用一个副本给第一夫人的话,下面的方法将被用到:
Address toAddress = new InternetAddress("vice.president@whitehouse.gov");
Address ccAddress = new InternetAddress("first.lady@whitehouse.gov");
message.addRecipient(Message.RecipientType.TO, toAddress);
message.addRecipient(Message.RecipientType.CC, ccAddress);

JavaMail API并没有提供检查邮件地址有效性的机制。当然我们可以自己完成这个功能:验证邮件地址的字符是否按照RFC822规定的格式书写或者通过DNS服务器上的MX记录验证等。

E.Authenticator
像java.net类那样,JavaMail API通过使用授权者类(Authenticator)以用户名、密码的方式访问那些受到保护的资源,在这里“资源”就是指邮件服务器。在 javax.mail包中可以找到这个JavaMail的授权者类(Authenticator)。
在使用Authenticator这个抽象类时,我们必须采用继承该抽象类的方式,并且该继承类必须具有返回 PasswordAuthentication对象(用于存储认证时要用到的用户名、密码)getPasswordAuthentication()方 法。并且要在Session中进行注册,使Session能够了解在认证时该使用哪个类。
下面代码片断中的MyAuthenticator就是一个Authenticator的子类。
Properties props = new Properties();
// fill props with any information
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);


F.Transport
在发送信息时,Transport类将被用到。这个类实现了发送信息的协议(通称为SMTP),此类是一个抽象类,我们可以使用这个类的静态方法send()来发送消息:
Transport.send(message);

当然,方法是多样的。我们也可由Session获得相应协议对应的Transport实例。并通过传递用户名、密码、邮件服务器主机名等参数建立与邮件服务器的连接,并使用sendMessage()方法将信息发送,最后关闭连接:
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

评论:上面的方法是一个很好的方法,尤其是在我们在同一个邮件服务器上发送多个邮件时。因为这时我们将在连接邮件服务器后连续发送邮件,然后再关闭掉连 接。send()这个基本的方法是在每次调用时进行与邮件服务器的连接的,对于在同一个邮件服务器上发送多个邮件来讲可谓低效的方式。
注意:如果需要在发送邮件过程中监控mail命令的话,可以在发送前设置debug标志:
session.setDebug(true)。


G.Store和Folder
接收邮件和发送邮件很类似都要用到Session。但是在获得Session后,我们需要从Session中获取特定类型的Store,然后连接到 Store,这里的Store代表了存储邮件的邮件服务器。在连接Store的过程中,极有可能需要用到用户名、密码或者Authenticator。
// Store store = session.getStore("imap");
Store store = session.getStore("pop3");
store.connect(host, username, password);

在连接到Store后,一个Folder对象即目录对象将通过Store的getFolder()方法被返回,我们可从这个Folder中读取邮件信息:
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();

上面的例子首先从Store中获得INBOX这个Folder(对于POP3协议只有一个名为INBOX的Folder有效),然后以只读 (Folder.READ_ONLY)的方式打开Folder,最后调用Folder的getMessages()方法得到目录中所有Message的数 组。

注意:对于POP3协议只有一个名为INBOX的Folder有效,而对于IMAP协议,我们可以访问多个Folder(想想前面讲的IMAP协议)。而 且SUN在设计Folder的getMessages()方法时采取了很智能的方式:首先接收新邮件列表,然后再需要的时候(比如读取邮件内容)才从邮件 服务器读取邮件内容。
在读取邮件时,我们可以用Message类的getContent()方法接收邮件或是writeTo()方法将邮件保存,getContent()方法只接收邮件内容(不包含邮件头),而writeTo()方法将包括邮件头。
System.out.println(((MimeMessage)message).getContent());

在读取邮件内容后,别忘记了关闭Folder和Store。
folder.close(aBoolean);
store.close();

传递给Folder.close()方法的boolean 类型参数表示是否在删除操作邮件后更新Folder。

H.继续向前进!
在讲解了以上的七个Java Mail核心类定义和理解了简单的代码片断后,下文将详细讲解怎样使用这些类实现JavaMail API所要完成的高级功能。

五、使用JavaMail API
在明确了JavaMail API的核心部分如何工作后,本人将带领大家学习一些使用Java Mail API任务案例。
1.发送邮件
在获得了Session后,建立并填入邮件信息,然后发送它到邮件服务器。这便是使用Java Mail API发送邮件的过程,在发送邮件之前,我们需要设置SMTP服务器:通过设置Properties的mail.smtp.host属性。
String host = ...;
String from = ...;
String to = ...;

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
Transport.send(message);

由于建立邮件信息和发送邮件的过程中可能会抛出异常,所以我们需要将上面的代码放入到try-catch结构块中。

2.接收邮件
为了在读取邮件,我们获得了session,并且连接到了邮箱的相应store,打开相应的Folder,然后得到我们想要的邮件,当然别忘记了在结束时关闭连接。
String host = ...;
String username = ...;
String password = ...;

// Create empty properties
Properties props = new Properties();

// Get session
Session session = Session.getDefaultInstance(props, null);

// Get the store
Store store = session.getStore("pop3");
store.connect(host, username, password);

// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

// Get directory
Message message[] = folder.getMessages();

for (int i=0, n=message.length; i
上面的代码所作的是从邮箱中读取每个邮件,并且显示邮件的发信人地址和主题。从技术角度讲,这里存在着一个异常的可能:当发信人地址为空时,getFrom()[0]将抛出异常。

下面的代码片断有效的说明了如何读取邮件内容,在显示每个邮件发信人和主题后,将出现用户提示从而得到用户是否读取该邮件的确认,如果输入YES的话,我 们可用Message.writeTo(java.io.OutputStream os)方法将邮件内容输出到控制台上,关于Message.writeTo()的具体用法请看JavaMail API。
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));

// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i

3.删除邮件和标志
设置与message相关的Flags是删除邮件的常用方法。这些Flags表示了一些系统定义和用户定义的不同状态。在Flags类的内部类Flag中预定义了一些标志:
Flags.Flag.ANSWERED
Flags.Flag.DELETED
Flags.Flag.DRAFT
Flags.Flag.FLAGGED
Flags.Flag.RECENT
Flags.Flag.SEEN
Flags.Flag.USER
但需要在使用时注意的:标志存在并非意味着这个标志被所有的邮件服务器所支持。例如,对于删除邮件的操作,POP协议不支持上面的任何一个。所以要确定哪 些标志是被支持的——通过访问一个已经打开的Folder对象的getPermanetFlags()方法,它将返回当前被支持的Flags类对象。
删除邮件时,我们可以设置邮件的DELETED标志:
message.setFlag(Flags.Flag.DELETED, true);

但是首先要采用READ_WRITE的方式打开Folder:
folder.open(Folder.READ_WRITE);

在对邮件进行删除操作后关闭Folder时,需要传递一个true作为对删除邮件的擦除确认。
folder.close(true);

Folder类中另一种用于删除邮件的方法expunge()也同样可删除邮件,但是它并不为sun提供的POP3实现支持,而其它第三方提供的POP3实现支持或者并不支持这种方法。
另外,介绍一种检查某个标志是否被设置的方法:Message.isSet(Flags.Flag flag)方法,其中参数为被检查的标志。

4.邮件认证
我们在前面已经学会了如何使用Authenticator类来代替直接使用用户名和密码这两字符串作为 Session.getDefaultInstance()或者Session.getInstance()方法的参数。在前面的小试牛刀后,现在我们将 了解到全面认识一下邮件认证。
我们在此取代了直接使用邮件服务器主机名、用户名、密码这三个字符串作为连接到POP3 Store的方式,使用存储了邮件服务器主机名信息的属性文件,并在获得Session时传入自定义的Authenticator实例:
// Setup properties
Properties props = System.getProperties();
props.put("mail.pop3.host", host);

// Setup authentication, get session
Authenticator auth = new PopupAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

// Get the store
Store store = session.getStore("pop3");
store.connect();


PopupAuthenticator类继承了抽象类Authenticator,并且通过重载Authenticator类的 getPasswordAuthentication()方法返回PasswordAuthentication类对象。而 getPasswordAuthentication()方法的参数param是以逗号分割的用户名、密码组成的字符串。
import javax.mail.*;
import java.util.*;

public class PopupAuthenticator extends Authenticator {

public PasswordAuthentication getPasswordAuthentication(String param) {
String username, password;

StringTokenizer st = new StringTokenizer(param, ",");
username = st.nextToken();
password = st.nextToken();

return new PasswordAuthentication(username, password);
}

}


5.回复邮件
回复邮件的方法很简单:使用Message类的reply()方法,通过配置回复邮件的收件人地址和主题(如果没有提供主题的话,系统将默认将“Re:” 作为邮件的主体),这里不需要设置任何的邮件内容,只要复制发信人或者reply-to到新的收件人。而reply()方法中的boolean参数表示是 否将邮件回复给发送者(参数值为false),或是恢复给所有人(参数值为true)。
补充一下,reply-to地址需要在发信时使用setReplyTo()方法设置。
MimeMessage reply = (MimeMessage)message.reply(false);
reply.setFrom(new InternetAddress("president@whitehouse.gov"));
reply.setText("Thanks");
Transport.send(reply);


6.转发邮件
转发邮件的过程不如前面的回复邮件那样简单,它将建立一个转发邮件,这并非一个方法就能做到。
每个邮件是由多个部分组成,每个部分称为一个邮件体部分,是一个BodyPart类对象,对于MIME类型邮件来讲就是MimeBodyPart类对象。 这些邮件体包含在成为Multipart的容器中对于MIME类型邮件来讲就是MimeMultiPart类对象。在转发邮件时,我们建立一个文字邮件体 部分和一个被转发的文字邮件体部分,然后将这两个邮件体放到一个Multipart中。说明一下,复制一个邮件内容到另一个邮件的方法是仅复制它的 DataHandler(数据处理者)即可。这是由JavaBeans Activation Framework定义的一个类,它提供了对邮件内容的操作命令的访问、管理了邮件内容操作,是不同的数据源和数据格式之间的一致性接口。
// Create the message to forward
Message forward = new MimeMessage(session);

// Fill in header
forward.setSubject("Fwd: " + message.getSubject());
forward.setFrom(new InternetAddress(from));
forward.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(
"Here you go with the original message: ");

// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Create and fill part for the forwarded content
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(message.getDataHandler());

// Add part to multi part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
forward.setContent(multipart);

// Send message
Transport.send(forward);


7.使用附件
附件作为与邮件相关的资源经常以文本、表格、图片等格式出现,如流行的邮件客户端一样,我们可以用JavaMail API从邮件中获取附件或是发送带有附件的邮件。

A.发送带有附件的邮件
发送带有附件的邮件的过程有些类似转发邮件,我们需要建立一个完整邮件的各个邮件体部分,在第一个部分(即我们的邮件内容文字)后,增加一个具有DataHandler的附件而不是在转发邮件时那样复制第一个部分的DataHandler。

如果我们将文件作为附件发送,那么要建立FileDataSource类型的对象作为附件数据源;如果从URL读取数据作为附件发送,那么将要建立URLDataSource类型的对象作为附件数据源。

然后将这个数据源(FileDataSource或是URLDataSource)对象作为DataHandler类构造方法的参数传入,从而建立一个DataHandler对象作为数据源的DataHandler。

接着将这个DataHandler设置为邮件体部分的DataHandler。这样就完成了邮件体与附件之间的关联工作,下面的工作就是BodyPart的setFileName()方法设置附件名为原文件名。

最后将两个邮件体放入到Multipart中,设置邮件内容为这个容器Multipart,发送邮件。
// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail Attachment");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText("Pardon Ideas");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);

如果我们使用servlet实现发送带有附件的邮件,则必须上传附件给servlet,这时需要注意提交页面form中对编码类型的设置应为multipart/form-data。





B.读取邮件中的附件
读取邮件中的附件的过程要比发送它的过程复杂一点。因为带有附件的邮件是多部分组成的,我们必须处理每一个部分获得邮件的内容和附件。
但是如何辨别邮件信息内容和附件呢?Sun在Part类(BodyPart类实现的接口类)中提供了getDisposition()方法让开发者获得邮 件体部分的部署类型,当该部分是附件时,其返回之将是Part.ATTACHMENT。但附件也可以没有部署类型的方式存在或者部署类型为 Part.INLINE,无论部署类型为Part.ATTACHMENT还是Part.INLINE,我们都能把该邮件体部分导出保存。
Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i
下列代码中使用了saveFile方法是自定义的方法,它根据附件的文件名建立一个文件,如果本地磁盘上存在名为附件的文件,那么将在文件名后增加数字表示区别。然后从邮件体中读取数据写入到本地文件中(代码省略)。
// from saveFile()
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
}

以上是邮件体部分被正确设置的简单例子,如果邮件体部分的部署类型为null,那么我们通过获得邮件体部分的MIME类型来判断其类型作相应的处理,代码结构框架如下:
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
// Handle plain
} else {
// Special non-attachment cases here of
// image/gif, text/html, ...
}
...
}


8.处理HTML邮件
前面的例子中发送的邮件都是以文本为内容的(除了附件),下面将介绍如何接收和发送基于HTML的邮件。
A.发送HTML邮件
假如我们需要发送一个HTML文件作为邮件内容,并使邮件客户端在读取邮件时获取相关的图片或者文字的话,只要设置邮件内容为html代码,并设置内容类型为text/html即可:
String htmlText = "

Hello

" +
"";
message.setContent(htmlText, "text/html"));

请注意:这里的图片并不是在邮件中内嵌的,而是在URL中定义的。邮件接收者只有在线时才能看到。
在接收邮件时,如果我们使用JavaMail API接收邮件的话是无法实现以HTML方式显示邮件内容的。因为JavaMail API邮件内容视为二进制流。所以要显示HTML内容的邮件,我们必须使用JEditorPane或者第三方HTML展现组件。

以下代码显示了如何使用JEditorPane显示邮件内容:
if (message.getContentType().equals("text/html")) {
String content = (String)message.getContent();
JFrame frame = new JFrame();
JEditorPane text = new JEditorPane("text/html", content);
text.setEditable(false);
JScrollPane pane = new JScrollPane(text);
frame.getContentPane().add(pane);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.show();
}


B.在邮件中包含图片
如果我们在邮件中使用HTML作为内容,那么最好将HTML中使用的图片作为邮件的一部分,这样无论是否在线都会正确的显示HTML中的图片。处理方法就 是将HTML中用到的图片作为邮件附件并使用特殊的cid URL作为图片的引用,这个cid就是对图片附件的Content-ID头的引用。
处理内嵌图片就像向邮件中添加附件一样,不同之处在于我们必须通过设置图片附件所在的邮件体部分的header中Content-ID为一个随机字符串,并在HTML中img的src标记中设置为该字符串。这样就完成了图片附件与HTML的关联。
String file = ...;

// Create the message
Message message = new MimeMessage(session);

// Fill its headers
message.setSubject("Embedded Image");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "

Hello

" +
"";
messageBodyPart.setContent(htmlText, "text/html");

// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);

// Create part for the image
messageBodyPart = new MimeBodyPart();

// Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","");

// Add part to multi-part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
message.setContent(multipart);


9.在邮件中搜索短语
JavaMail API提供了过滤器机制,它被用来建立搜索短语。这个短语由javax.mail.search包中的SearchTerm抽象类来定义,在定义后我们便可以使用Folder的Search()方法在Folder中查找邮件:
SearchTerm st = ...;
Message[] msgs = folder.search(st);

下面有22个不同的类(继承了SearchTerm类)供我们使用:
AND terms (class AndTerm)
OR terms (class OrTerm)
NOT terms (class NotTerm)
SENT DATE terms (class SentDateTerm)
CONTENT terms (class BodyTerm)
HEADER terms (FromTerm / FromStringTerm, RecipientTerm / RecipientStringTerm, SubjectTerm, etc.)
使用这些类定义的断语集合,我们可以构造一个逻辑表达式,并在Folder中进行搜索。下面是一个实例:在Folder中搜索邮件主题含有“ADV”字符串或者发信人地址为friend@public.com的邮件。
SearchTerm st =
new OrTerm(
new SubjectTerm("ADV:"),
new FromStringTerm("friend@public.com"));
Message[] msgs = folder.search(st);


六、参考资源
JavaMail API Home
Sun’s JavaMail API基础
JavaBeans Activation Framework Home
javamail-interest mailing list
Sun's JavaMail FAQ
jGuru's JavaMail FAQ
Third Party Products List

七、代码下载
http://java.sun.com/developer/onlineTraining/JavaMail/exercises.html

2012年2月21日星期二

Windows 7 - Notices but does not 'see' external usb hard drive

Windows 7 - Notices but does not 'see' external usb hard drive


The resolution was to:

  1. Plug in and turn on the external USB hard drive
  2. Right mouse click on My Computer
  3. Left click on Manage
  4. Left click on Device Manager
  5. Expand the USB list
  6. Find the USB device for your USB Hard Drive (in my case it was the one that had no description)
  7. Right mouse click - Uninstall
  8. Turn off the external USB hard drive
  9. Turn on the external USB hard drive and let it find and install the driver
  10. It should work

How to run Unix shell script from java code? - Stack Overflow

How to run Unix shell script from java code? - Stack Overflow

You should really look at Process Builder. It is really built for this kind of thing.

ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");  Map env = pb.environment();  env.put("VAR1", "myValue");  env.remove("OTHERVAR");  env.put("VAR2", env.get("VAR1") + "suffix");  pb.directory(new File("myDir"));  Process p = pb.start(); 

2012年2月19日星期日

How to Kill Fire Ants and Commit Genocide

How to Kill Fire Ants and Commit Genocide

This Instructable is a glimmer of hope for the haters of that most dastardly of ants, the FIRE ANT

Step 1A Brief Word:

Now, before you jump to conclusions, I believe all animals have a purpose and place (generally right next to the mashed potatoes and gravy :D), however when they are no longer in this place, they no longer have purpose. Red Imported Fire Ants are an example of these rouge species (they're imported, duh). I quite personally detest the little bastards, they sting you, they destroy native habitats, they devour native species, they are a general pain, and they bite you (I might have mentioned that one already). So anyway, it is one of my life's passions to destroy as many of there kind as possible. However, I seek only to destroy fire ants, leading me to come to various means of mound specific destruction.

Step 2Let's get started

One fine day I was making a vinegar and baking soda volcano. I was directed by a higher power (known simply as MUM) to set it off outside. I craftily placed it on a fire ant mound and let it rip. I was surprised at the massive mayhem and destruction it caused. Ants poured out of the mound, hoping to escape the burning acidity of the magma that rained down upon them, only to be fried by more of that unrelenting acid from hell, and my magnifying glass. Upon demonstrating this new weapon to the mighty MUM, I was commanded to go forth and destroy as many of the Mounds of the Red Infidels as possible.

Further use crafted this WMD into the form I'll show you today.

First thing you do is find you a fire ant mound, some sodium bicarbonate (baking soda jock), and the most powerful vinegar(usually 20% can be found in garden stores) you can lay thine destructive hands upon.

Weird, it won't let me add notes to my pic. Anyways, due to MUM's (and mine own) mighty wrath, all the best Mounds of the Red Infidels were destroyed before I thought to pass this information onto others. Therefore I am forced to apologise for the shrimpy mound.

Step 3Name this step ....... umm dunno, Next Step?

  • C:\MM II\Documents. Pics & Paper\Holding\Ant2.jpg
  • C:\MM II\Documents. Pics & Paper\Holding\Ant3.jpg
Generously sprinkle the mound with baking soda. If large, poke holes in the mound and fill 'em with baking soda. I haven't confirmed it, but it seems the ants don't care to much for the baking soda.

Step 4Step... Dunno, wasn't keeping count

  • C:\MM II\Documents. Pics & Paper\Holding\Ant5.jpg
  • C:\MM II\Documents. Pics & Paper\Holding\Ant4.jpg
  • C:\MM II\Documents. Pics & Paper\Holding\Ant6.jpg
After the mound has been treated with sodium bicarbonate, you're ready to add vinegar. Pour a generous amount of vinegar on the mound, a small mound (like the one illustrated) takes about a quart, larger ones up to a half a gallon. Watch as those filthsome and foulsome little orcses die my precious, they DIE my precious, die!!! mmmuuuhhahhahahaha!!!!. Sorry, it's just I found this ring in a hunk of volcanic rock and it's really weird what it'll get you to say...
Anyway, the ants will get fried alive as the vinegar eats away at there little exoskeletons, propelled deeper into the mound by the reacting baking soda. Not sure but it's possible they get smothered by the CO2 produced by the reaction. Either way, you'll have a mound full of dead fire ants in no time.

Excuse while I investigate the screams outside...

OMG!! GIANT KILLER FIRE ANTS??? THEY ARE COMING FOR ME!!! AAAAAGGGHHH!!! THEY BITE!!!! HELLPPPPPPPPPP!!!!! AAAAAAAAGGGGGHHHHH gulph (assorted sounds of expiration)

live... long... and prosper... guhh ehh
RocketScientist2015

2012年2月17日星期五

给大家推荐一些有助于提高英语的节目[Chinese In North America(北美华人e网)]

给大家推荐一些有助于提高英语的节目[Chinese In North America(北美华人e网)]


看隔壁的帖子说英语不好被嘲笑的经历。我觉得嘲笑可能严重了,但因为发音不准,表达不清楚或者干脆表达不出来的时候很多,比较影响和同事朋友的交流(不过他们从不嘲笑我,呵呵)。因此觉得有必要发这个帖子。

2012年2月16日星期四

java Date 按照指定格式输出。 - hiaurora - 博客大巴

java Date 按照指定格式输出。 - hiaurora - 博客大巴


java的Date按照指定格式输出:
Date startTime = new Date();//当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String startStr = sdf.format(startTime);
很多时候用到,但总是忘记了,尤其是年月日,时分秒,毫秒的大小写。
特此记录一下,^_^


从字符串产生DATE对象
SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
cldate = dformat.parse(datestring);

强制tomcat使用https访问 « DDL's Space

强制tomcat使用https访问 « DDL's Space

Web Development Tips

Web Development Tips

1.    Tomcat

a)    FAIL - Application at context path /app could not be started"

When redeploy your webapp via tomcat manager, sometimes tomcat seems not able to copy .war to deploy correctly. After click deploy button, if you check webapps/yourapp.war content, you will find it still use old version. So you have to manually copy your .war file to webapps/ and unrar it. Here is how to do it

1. suppose you have aa.war at somewhere/
2. mkdir aa
3. cd aa
4. jar -xvf aa.war
5. cd webapps
6.cp -r somewhere/aa .
7. restart tomcat

I just found another faster way to do it. In eclipse, when exporting war file, directly select the tomcat webapps dir. The tomcat will automatically deploy it, so you don’t need to use tomcat manager to deploy

b)    数字证书和数字签名

数字证书:证书包含了公钥
数字签名:用私钥加密了的messagemd5
https过程:浏览器发出https请求 | 网站送出数字证书+用私钥加密了的文本 | 浏览器验证数字证书是否经过第三方认证 | 如果是,安装证书,解密文本 | 浏览器送出用证书中公钥加密了的用户名和密码 + 数字签名 | 网站用用户的公钥解密数字签名 | 如成功 则对比文本的md5 | 如文本没有篡改 进行下一步

c)     One-Way Https Configuration

第一步:Tomcat5.5使用SSL功能、证书生成(使用JDK1.5自带的keytool.exe工具)
注意必须使用tomcat使用的jdk版本去生成
命令行中C:\Java\jdk1.5.0_09\bin> keytool -genkey -alias tomcat -keyalg RSA -validity 365
注:-alias tomcat 设定别名;   -validity 365 证书有效期设为365天;
生成证书过程中,提示设定<tomcat>的密码时,使用和keystore相同的密码。默认回车就可以了。
生成的证书默认是放在当前系统用户的主目录下(C:\Documents and Settings\Administrator),复制至Tomcat的主目录以便于下一步中的server.xmlkeystoreFile的值设定。



第二步:Tomcat5.0.28server.xml,打开SSL连接器选项
  <Connector port="8443" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               keystoreFile=".keystore"  keystorePass="******"
               clientAuth="false" sslProtocol="TLS"/>
注:即添加后面那两属性值,keystoreFile的路径,keystorePass的密码。

重起Tomcat服务器后, 就只能用HTTPS协议访问了, 只是端口是8443, 需要在URL中显示指定. 最后,https 的默认端口是443 (Usually is taken by apache)

d)    强制https访问

  tomcat\conf\web.xml中的</welcome-file-list>后面加上这样一段:appweb.xml里也可以

    <login-config> 
        <!-- Authorization setting for SSL --> 
        <auth-method>CLIENT-CERT</auth-method> 
        <realm-name>Client Cert Users-only Area</realm-name> 
    </login-config> 
    <security-constraint> 
        <!-- Authorization setting for SSL --> 
        <web-resource-collection > 
            <web-resource-name >SSL</web-resource-name> 
            <url-pattern>/*</url-pattern> 
        </web-resource-collection> 
        <user-data-constraint> 
            <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
        </user-data-constraint> 
    </security-constraint> 

完成以上步骤后,在浏览器中输入http的访问地址也会自动转换为https

e)     Two-Way Https Configuration And More Details



2.    MySql

a)    Database Permission Setup

Please Type the following in Linux console

mysql -p -u root
grant all on phlex.* to 'username'@'%';
show grants for username@'%';
grant select,insert,update on phlex.* to 'osqa'@'localhost';
show grants for osqa@localhost;

b)    Show Database Schema in Eclipse

1. Switch to “Database Development” Perspective
2. Creating a new Database Connection: add database name at the end of your URL, Some thing like: jdbc:mysql://yourhost:3306/phlex. Otherwise you won’t see the columns of tables.

3.    Eclipse

a)    Deployment or Export war file in Eclipse

You can view and modify the setup in right click of Your Project à Deployment Assembly. Default setup is
Copy all stuff under WebContent
Copy all .class under /src to WEB-INF/classes

b)    Using external Jar Libraries

In eclipse, you need libraries in two places:
1) To compile java files. These libraries are shown in YourProject/JavaResource/Libraries. You can add them via right click of YourProject à Java Build Path à Add External Jars OR Add Libraries. Here Jars means individual libs while Libraries are group of libs
2) To run .class file on your http server. These libraries are located in WEB-INF/lib. You can add your Libraries into it as well.
So the best way to do it is as follows
a) Create User Libraries to contain all external libraries: Window à Perferences à Java à Build Path à User Libraries à New
b) Add this Library into your project for build
c) Add this Library into your War file for http server

4.    HTML

5.    CSS

a)    Force CSS changes to “go live” immediately

If you update your .css file, you may have noticed that you have to “force-reload” your site in your browser to see the changes. This is because your browser keeps a copy of the CSS cached on your hard drive. Depending on how your server is set up, it may not check for a new version of the stylesheet for a couple hours, or longer! One way to solve this is to “version” your CSS file, by adding ?v=123 to the URL in the to your stylesheet.

<link rel="stylesheet" type="text/css" href="mystyle.css?v=1.1" />

b)    Control table and its content position using CSS

table{
       border-collapse: collapse;
       border: 1px solid gray;
       width:1px;
       white-space:nowrap
       text-align: left;
       margin-left:auto;                èwhole table center
    margin-right:auto;                     èwhole table center
}

6.    Hibernate

a)    Install Hibernate Tool Plug-in in Eclipse Indigo

1. When Eclipse starts, go to Help > Install New Software...
2. Paste in the URL of the update site you've chosen, such as:
    http://download.jboss.org/jbosstools/updates/development/indigo/ or
    http://download.jboss.org/jbosstools/updates/development/indigo/soa-tooling/
3. Select all in “JBOSS Tools”, Hibernate in “Application Development”, “Data Services”, “Web and Java EE Development”
4. Install

b)    Using Hibernate in Eclipse Indigo

1. Create your Java EE project
2. Create a database and table in MySql
3. Create a database connection in eclipse’s Database Development perspective
4. Switch to eclipse’s Hinbernate perspecitve
5. Create Hibernate Configuration file by: File à New à Other à Hibernate à Hibernate Configuration File and place the hibernate.cfg.xml at the src root of your Java EE project.
6. Create consol configuration which defines how hibernate works.
7. Create POJO class by reveseing engineering using hibernate tools: File à  New à Hibernate à Hibernate Reverse Engineering file:  Select the table you want to reverse engeering. And then click tool bar icon ( run new configuration) to create pojo class automatically
8. Place hbm.xml file with your pojo class
Details are on
http://www.packtpub.com/article/developing-applications-with-jboss-and-hibernate-1

c)     Get the ID after Save

The object is automatically filled with the ID after save

7.    Java

a)    Date, SimpleDateFormat and Caleander

JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理。

Date startTime = new Date();//当前时间
System.out.println(startTime); // Sun Mar 08 16:35:58 CST 2009
Date endTime = new Date(startTime.toTime() + 60 * 60 * 24 * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String endStr = sdf.format(endTime);
System.out.println(endStr); // 2009-03-09 16:35:38.1234

Calendar c = Calendar.getInstance(); //当前时间
int month = c.get(Calendar.MONTH) + 1;
c.add(Calendar.DATE, 100);

Date d = c.getTime();
Calendar c = Calendar.getInstance();
c.setTime(d);

8.    Java Mail

9.    Java System

10.           Java Files





b) Control table and its content position using CSS


table{
       border-collapse: collapse;
       border: 1px solid gray;
       width:1px;
       white-space:nowrap
       text-align: left;
       margin-left:auto;                ==>whole table center
      margin-right:auto;                  ==>whole table center
}

TomCAT error msg "FAIL - Application at context path /app could not be started"

tomcat seems not able to copy .war to deploy correctly. After deploy, if you check .war content, you will find it still use old version of .war file. So you have to manually copy your .war file to webapps/ and unrar it. Here is how to do it

1. suppose you have aa.war at somewhere/
2. mkdir aa
3. cd aa
4. jar -xvf aa.war
5. cd webapps
6.cp -r somewhere/aa .
7. restart tomcat



I just found another faster way to do it. In eclipse, when exporting war file, directly select the tomcat webapps dir. The tomcat will automatically deploy it, so you don’t need to use tomcat manager to deploy