2006-05-27

DWR是个好东西

DWR

如果你有个POJO写成下面的格式:

public class DwrMagic
{
public String reply()
{
return "oh, dwr magic !";
}
}

你需要通过javascript访问到这个方法并展示在页面中,容易么?DWR告诉你,相当容易:
DwrMagic.reply(function(replycontent){alert(replycontent);}

Woo~ Cool~

下面讲一下具体实施办法:
下载一个dwr.jar放到WEB-INF/lib目录下,然后修改web.xml,添加个servlet:
不贴代码了,用blogger的编辑器编辑的时候有些麻烦,它自动处理代码,即使你不愿意它处理,看dwr上的getstart吧
然后再创建个dwr.xml放在web.xml同目录下,并把自己创建的类的信息写进来:
不贴代码了,用blogger的编辑器编辑的时候有些麻烦,它自动处理代码,即使你不愿意它处理,看dwr上的getstart吧
然后建立个testdwr.html文件:
把这几个script文件连接进来:
/dwr/dwr/interface/Demo.js
/dwr/dwr/engine.js
然后再写个javascript脚本函数:

function getData(str)
{
alert(str);
}
function getReply()
{
Demo.reply(getData);
}

然后随便找个地方调用getReply()就可以看到效果了,非常cool~

2006-05-25

代用户与server交互

Cookie怎么保存

JDK里没有参考实现,只有一个CookieHandler,而且是全局的,也就是说在一个JVM内你只能代表一个用户,后来找到个JCookie,不过操作起来挺麻烦的,后来感觉好像还和IE有关联,因为我没开IE的时候它就运行出错,开着就一切OK =_=#!

最后找到一个很好的:HttpClient,是apache的,早找到就好了,T_T,挺好用的,而且还带了些sample,常用的功能基本都能展示出来,暂时先不贴代码....

2006-05-24

建立ssl连接并通过BASIC认证

贴段代码了,没啥好说的哦

public void testHttpsConnection() throws Exception
{
URL url = new URL("https://www.blogger.com/atom");
X509TrustManager xtm = new X509TrustManager()
{
public void checkClientTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public void checkServerTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public X509Certificate[] getAcceptedIssuers()
{
X509Certificate[] cert = {};
return cert;
}
};
TrustManager mytm[] = {xtm};
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null,mytm, null );
SSLSocketFactory sf = ctx.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sf);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
String usernamePassword = "username" + ":" + "password";
String encoding = new BASE64Encoder().encode(usernamePassword.getBytes());
conn.setRequestProperty("Authorization", "BASIC " + encoding);
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IoUtil.transfer(in, out);
System.out.println(new String(out.toByteArray()));
}

把用户名和密码改掉了,嘿。只要你注册一个blogger的账号就可以顺利获得结果 :)

还有,blogger的atom API在这

2006-05-23

从群里偷偷瞄来的,收藏下~

Re: SSLHandshakeException: could not find trusted certificate
Sep 2, 2002 6:40 AM (reply 1 of 12)



if you work in windows in control panel you have java plugin -
there is column certificates then you click on secure site and import public key.

in unix execute ControlPanel from your java direcotry and makes the same.
it could help.

l33t0n3
Posts:5
Registered: 9/3/02 Re: SSLHandshakeException: could not find trusted certificate
Sep 3, 2002 9:31 AM (reply 2 of 12)



There are two ways to fix your problem. The first is add the certificate(s) of the target site to your TrustStore.

In the JSSE, the TrustStore object is a Keystore file which contains the public key(s) and any Root keys for a server certificate. You can add the key to your truststore by using keytool. (read JSSE documentation http://java.sun.com/products/jsse/doc/guide/API_users_guide.html to learn more about keytool):

In order to do this you will need to get the servers public (not private) key and any root certificates and then use the keytool.

Unfortunately, most of the time this is way too much of a pain in the butt to do for every ssl connection. For this reason, you can actually create your own TrustManager implementaiton and assign it to the SSLSocketFactory. The SSLSocketFactory can either be used by you directly, or you can let the java.net.URL connection factory handle the fetching and reading of sockets for you. I highly recommend using java.net.URL because it will load the https handler and you won't have to worry about writing the http requests properly.

First, you must create an implementation of the TrustManager factory. I recommend that for your first go you just trust everything (i.e. the isServerTrusted(X509Certificate[] Servers) method should always return true).

Make sure you have specified your provider (system property or dynamically as shown (this is jdk1.3 w/ JSSE 1.0.3, with jdk 1.4 it s bit different)...

java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());

get your ssl context...
ctx = SSLContext.getInstance("TLS");

create an array of 1 object containing your TrustManager...
TrustManager[] _trustm = {new your.BogusX509TrustManager()};

And then tell the context to init using your trust manager...
ctx.init(null, _trustm, null);

make that your default socket factory for ssl connections...
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

Now your SocketFactory (and TrustManager) will be used for any new https connection you open in this instance. Since you set isServerTrusted to always return true, you will trust all certificates. You can also go a bit farther and validate certificates and store certificates in your TrustStore...

2006-05-22

dom4j解析的时候乱码

link: "java中文乱码解决方案和经验"

用DOM4J读节点数据的时候老出现中文乱码,我开始以为是DOM4J的问题,然后就查,最后看到一个解决方案,感觉不太爽,然后就接着找,后来不知道怎么就发现原来错误不在DOM4J。

主要是我读文件后把它存成byte[]格式的了,然后直接
byte[] gdpBytes = retrieveBytes();
String text = new String(gdpBytes);
Document doc = DocumentHelper.parseText(text);


这样是不对滴,就在第二行出的问题,这个时候还需要作一下编码声明处理:
String text = new String(gdpBytes,"utf-8");

这样就OK了。还有几个连接,虽然没用到,但是以后可能有用:
1 2

2006-05-19

hibernate中的binary

在mapping文件中声明了一项用来存储文件的属性“bytes”:





实际上是把bytes映射成数据库表中的varchar,然后看了一下mysql的文档,varchar和binary是一样的属性,但是为什么是varchar而不直接binary就不明白了。但是varchar显然不够用于存储文件,执行存储的时候报错说
Data truncation: Data too long for column 'bytes' at row 1
后来就把它改成varbinary,顺便把最大值设为55535,然后就通过了。

没错,不是65535,如果那样的话会报错说需要把字段类型转换为BLOB。

用的是hibernate2.1, mysqlconnector5, mysql5.0

2006-05-18

瞎忙活了

鼓捣了半天,虽然没明白怎么回事,但是还是出了结果了。

就是想通过hbm.xml产生.sql文件,目的就是产生数据库表。然后就使劲地看build.xml文件,愣没看出来啥错,但是编译就是不通过,后来把build.properties改了一下,还是有错,然后把我添加到buildpath的一些包啊什么的去掉,然后就一切OK了。

见鬼