2007-01-21
[原创]用tapestry4.0.x生成验证码
关键字: tapestry service 动态图片 验证码
用tapestry4.0.x生成验证码
首先写一个从IEngineService派生的类,用来产生需要的验证图片
==============================
hivemodule.xml文件增加以下内容
============================
在页面上调用
首先写一个从IEngineService派生的类,用来产生需要的验证图片
package org.itrun.cnbrn.view.pageclass.system;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.web.WebSession;
import org.itrun.cnbrn.ConstantVar;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ValidatorImageService implements IEngineService {
Log logger=LogFactory.getLog(this.getClass());
private HttpServletResponse response;
private ServletContext context;
private String TYPE="jpeg";
public void setResponse(HttpServletResponse response){
this.response = response;
}
public void setContext(ServletContext context){
this.context=context;
}
public ILink getLink(boolean arg0, Object arg1) {
return null;
}
public String getName() {
return "validatorImage";
}
public void service(IRequestCycle cycle) throws IOException {
Random random = new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++){
int x=random.nextInt(25);
x+=65;
sb.append(String.valueOf((char)x));
//sb.append(" ");
}
String validateString=sb.toString();
WebSession sess=cycle.getInfrastructure().getRequest().getSession(true);
sess.setAttribute(ConstantVar.VALIDATOR_SESSION_KEY, validateString);
//画布大小
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 以下填充背景颜色
g.setColor(Color.decode("#FFFFFF"));
g.fillRect(0, 0, width, height);
//随机产生其他元素,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 300; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
//字的颜色
g.setColor(Color.decode("#01556B"));
//写的字的大小
g.setFont(new Font(null, Font.BOLD, 18));
//在画布上写字
g.drawString(validateString, 2, 16);
g.dispose();
OutputStream os = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(image);
os.close();
}
private Color getRandColor(int fc,int bc){//给定范围获得随机颜色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
}
==============================
hivemodule.xml文件增加以下内容
<service-point id="ValidatorImageService" interface="org.apache.tapestry.engine.IEngineService">
<invoke-factory>
<construct class="org.itrun.cnbrn.view.pageclass.system.ValidatorImageService"/>
</invoke-factory>
</service-point>
<contribution configuration-id="tapestry.services.ApplicationServices">
<service name="validatorImage" object="service:ValidatorImageService"/>
</contribution>
============================
在页面上调用
<img jwcid="@Any" src="ognl:cnbrnContextPath+'/app?service=validatorImage'" />
评论
jiangyh52
2007-04-10
好东西!
mercyblitz
2007-04-10
呵呵,受教了。我也用Servlet做过!
okhero
2007-01-23
不错,几个月之前我是用Servlet来做的.
发表评论
- 浏览: 40577 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最新评论
-
经典正则表达式(希望给自 ...
很好.
-- by lirong1978sdo -
[原创]用tapestry4.0.x ...
好东西!
-- by jiangyh52 -
[原创]用tapestry4.0.x ...
呵呵,受教了。我也用Servlet做过!
-- by mercyblitz -
为什么各大银行的网上银行 ...
ray_linn 写道weiqingfei 写道ray_linn 写道gigix ...
-- by weiqingfei -
为什么各大银行的网上银行 ...
说来说去又回到开始的话题“没有绝对安全的系统”就能够推出“没必要选择比较安全的系 ...
-- by gigix






评论排行榜