古代有四大美人,各位热爱Java的大侠,可知Java也有四大美女?
如果被我问得一头雾水,那你就是有点out了。Java从开天辟地,进化到今天,可谓历尽苍桑,但是还是免费的。那为什么甲骨文还要花重金购买呢?原因很简单,甲骨文已经发现了Java的价值千金所在了。千金者,美女也。
Java发展至今天,也有点历史了,却很少有人为其四大美女著书立说,鄙人不才,看不过眼,就硬着头皮献美了,四大美女出场:
小可爱Applet;
后台老板Servlet;
掌门人Portlet;
小家碧玉MIDlet。
做为美女,难免骚首弄姿,所以她们的出场可能有不同的姿态,暂安排如下 :
- 害羞的小可爱——Applet刷卡器
- 无限风情的小甜甜——Applet聊天室
- 通情达理的接线生——Applet软电话
- 后台老板——无所不能的Servlet
- 惊现掌门人——Portlet闪亮登场
- 小家碧玉——智能设备MIDlet
各位香味相投的朋友,是不是心痒痒,想尽快一睹美女的芳容了?好吧,让小可爱先给大家亮一个pose。
一、害羞的小可爱——Applet刷卡器
用IC卡在刷卡器一放,"嘟"的一响,就刷了卡了。那么刷卡的程序是怎么设计的,下面我们用一个在WEB上的刷卡器作一介绍,Applet程序:
// import java.awt.*;
import java.applet. * ;
import java.net.URL;
import java.net.MalformedURLException;
public class ReadCard extends Applet implements Runnable{
private boolean isStandalone = false ;
private String urlroot = null ;
private String urlfor = null ;
private Thread readCard;
private String forward = null ;
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
public void init() {
this .urlroot = getDocumentBase().toString().substring( 0 ,getDocumentBase().toString().lastIndexOf( " / " ) + 1 );
this .urlfor = getDocumentBase().toString().substring(getDocumentBase().toString().lastIndexOf( " / " ));
this .forward = getParameter( " forward " );
}
public void start() {
readCard = new Thread( this );
readCard.start();
}
public void stop() {
log( "" );
readCard = null ;
}
public void run() {
Thread me = Thread.currentThread();
while (readCard == me) {
try {
ReadCardBean rc = new ReadCardBean();
CardActionForm card = new CardActionForm();
card = rc.getFromCard();
if (card.getDevice() <= 0 ){
log( " 读卡器初始化失败! " );
} else if (card.getStatus() == 1 ){
log( " 请使用正确IC卡! " );
} else if (card.getStatus() == 2 ){
log( " 装入密码失败! " );
} else if (card.getStatus() == 3 ){
log( " 校验密码失败! " );
} else if (card.getStatus() == 4 ){
log( " 读卡失败! " );
} else if (card.getStatus() == 5 ){
log( " 读卡器退出失败! " );
} else if (card.getStatus() == 6 ){
log( " 蜂鸣失败! " );
} else {
log( "" );
card.setRand(java.lang.Math.random());
sendToClient(card);
}
Thread.currentThread().sleep( 700 );
}
catch (InterruptedException e) {
log( " Run Error: " + e.toString());
}
catch (Exception et) {
log( " RunOther Error: " + et.toString());
}
}
}
public void sendToClient(CardActionForm card){
try {
if (forward != null && " accept " .equals(forward))
urlfor = " dealAcceptAction.do?method=acceptCard " ;
else if (forward != null && " flower " .equals(forward))
urlfor = " dealAction.do?method=acceptCard " ;
else if (forward != null && " test " .equals(forward))
urlfor = " readCardAction.do?method=startRead " ;
String prBuffer = "" ;
if (card.getPrBuffer() != null )
prBuffer = new String(card.getPrBuffer());
URL newurl = new URL(urlroot + urlfor + " &prBuffer= " + prBuffer + " &j " + card.getRand() + " = " );
getAppletContext().showDocument(newurl, " _self " );
} catch (MalformedURLException g){
log( " Send Error: " + g.toString());
}
}
public void log(String str){
getAppletContext().showStatus(str);
}
}
这个Applet运用了一个线程,等待刷卡操作,一旦刷卡成功,发出蜂鸣声并转到其它应用界面,否则在浏览器的状态栏中打出错误信息。Applet还调用了一个读卡程序和一个数据结构。
package dcrf;
class ReadCardBean
{
public void ReadCardBean(){
}
public CardActionForm getFromCard()
{
short status;
int lDevice = 0 ;
int [] pSnr = new int [ 1 ];
char [] pSBuffer = new char [ 16 ];
char [] pRBuffer = new char [ 16 ];
JavaRD800 rd = new JavaRD800();
CardActionForm form = new CardActionForm();
form.setStatus(( short ) 0 );
form.setDevice( 1 );
// 初始化读卡器
lDevice = rd.dc_init( 100 , 115200 );
if (lDevice <= 0 )
{
form.setDevice(lDevice);
return form;
}
// 检查卡类型
status = rd.dc_card(lDevice,( short ) 0 ,pSnr);
if (status != 0 )
{
form.setStatus(( short ) 1 );
rd.dc_exit(lDevice);
return form;
}
form.setPsnr(pSnr);
// 发出蜂鸣声
status = rd.dc_beep(lDevice,( short ) 10 );
if (status != 0 )
{
form.setStatus(( short ) 6 );
rd.dc_exit(lDevice);
return form;
}
// 装入密码
pSBuffer[ 0 ] = 0xCC ;
...
pSBuffer[ 6 ] = 0x00 ;
status = rd.dc_load_key(lDevice,( short ) 0 ,( short ) 1 ,pSBuffer);
if (status != 0 )
{
form.setStatus(( short ) 2 );
rd.dc_exit(lDevice);
return form;
}
form.setPsBuffer(pSBuffer);
// 验证密码
status = rd.dc_authentication(lDevice,( short ) 0 ,( short ) 1 );
if (status != 0 )
{
form.setStatus(( short ) 3 );
rd.dc_exit(lDevice);
return form;
}
// 读出编码
status = rd.dc_read(lDevice,( short ) 4 ,pRBuffer);
if (status != 0 )
{
form.setStatus(( short ) 4 );
rd.dc_exit(lDevice);
return form;
}
form.setPrBuffer(pRBuffer);
// 退出
status = rd.dc_exit(lDevice);
if (status != 0 )
{
form.setStatus(( short ) 5 );
rd.dc_exit(lDevice);
return form;
}
return form;
}
}
package dcrf;
public class CardActionForm implements java.io.Serializable{
private short status;
private int device;
private int [] psnr;
private char [] psBuffer;
private char [] prBuffer;
private String owner_code;
private double rand;
public short getStatus() { return status; }
public void setStatus( short status) { this .status = status; }
public int getDevice() { return device; }
public void setDevice( int device) { this .device = device; }
public int [] getPsnr() { return psnr; }
public void setPsnr( int [] psnr) { this .psnr = psnr; }
public char [] getPsBuffer() { return psBuffer; }
public void setPsBuffer( char [] psBuffer) { this .psBuffer = psBuffer; }
public char [] getPrBuffer() { return prBuffer; }
public void setPrBuffer( char [] prBuffer) { this .prBuffer = prBuffer; }
public String getOwner_code() { return owner_code; }
public void setOwner_code(String owner_code) { this .owner_code = owner_code; }
public double getRand() { return rand; }
public void setRand( double rand) { this .rand = rand; }
}
读卡程序还调用了IC卡的Api:
package dcrf;
public class JavaRD800
{
public JavaRD800()
{
}
static
{
System.loadLibrary( " javaRD800 " );
}
public native int dc_init( int lPort, int lBaud);
public native short dc_exit( int lDevice);
public native short dc_card( int lDevice, short iMode, int [] pSnr);
public native short dc_authentication( int lDevice, short iMode, short iSecNr);
public native short dc_halt( int lDevice);
public native short dc_read( int lDevice, short iAdr, char [] pData);
public native short dc_write( int lDevice, short iAdr, char [] pData);
public native short dc_increment( int lDevice, short iAdr, int lValue);
public native short dc_decrement( int lDevice, short iAdr, int lValue);
public native short dc_initval( int lDevice, short iAdr, int lValue);
public native short dc_readval( int lDevice, short iAdr, int [] pValue);
public native short dc_transfer( int lDevice, short iAdr);
public native short dc_restore( int lDevice, short iAdr);
public native short dc_load_key( int lDevice, short iMode, short iSecNr, char [] pKey);
public native short dc_beep( int lDevice, short iMsec);
public native short dc_high_disp( int lDevice, short iOffset, short iDispLen, char [] pDispStr);
public native short dc_request( int lDevice, short iMode, int [] pTagType);
public native short dc_anticoll( int lDevice, short iBcnt, int [] pSnr);
public native short dc_select( int lDevice, int lSnr, short [] pSize);
public native short dc_gettime( int lDevice, char [] pTimeStr);
public native short dc_gettimehex( int lDevice, char [] pTimeStr);
public native short dc_settime( int lDevice, char [] pTimeStr);
public native short dc_settimehex( int lDevice, char [] pTimeStr);
public native short dc_setbright( int lDevice, short iBright);
public native short dc_ctl_mode( int lDevice, short iMode);
public native short dc_disp_mode( int lDevice, short iMode);
public native short dc_cpureset( int lDevice, short [] pLen, char [] pData);
public native short dc_cpuapdu( int lDevice, short iLen, char [] pSData, short [] pLen, char [] pRData);
public native short dc_cpuapdusource( int lDevice, short iLen, char [] pSData, short [] pLen, char [] pRData);
public native short dc_cpudown( int lDevice);
public native short dc_swr_eeprom( int lDevice, int lOffset, int lLen, char [] pBuffer);
public native short dc_srd_eeprom( int lDevice, int lOffset, int lLen, char [] pBuffer);
public native short dc_disp_str( int lDevice, char [] pDispStr);
}
把上面程序编译打包为:card.jar,然后在页面上调用:
<</span>/script>
从这个调用上我们可以看到,这个Applet没有显示在页面上,因为它的长和宽都设为:"0"了,width="0" height="0"。
这个小可爱可不是一般的"犹抱琵琶半遮面"的害羞,简直是只闻其声,不见其影。看来今天美女芳容还是难得一见,哈哈...