用tapestry4.0.x生成验证码

用tapestry4.0.x生成验证码

首先写一个从IEngineService派生的类,用来产生需要的验证图片

代码
  1. package org.itrun.cnbrn.view.pageclass.system;   
  2.   
  3. import java.awt.Color;   
  4. import java.awt.Font;   
  5. import java.awt.Graphics;   
  6. import java.awt.image.BufferedImage;   
  7. import java.io.IOException;   
  8. import java.io.OutputStream;   
  9. import java.util.Random;   
  10.   
  11. import javax.servlet.ServletContext;   
  12. import javax.servlet.http.HttpServletResponse;   
  13. import javax.servlet.http.HttpSession;   
  14.   
  15. import org.apache.commons.logging.Log;   
  16. import org.apache.commons.logging.LogFactory;   
  17. import org.apache.tapestry.IRequestCycle;   
  18. import org.apache.tapestry.engine.IEngineService;   
  19. import org.apache.tapestry.engine.ILink;   
  20. import org.apache.tapestry.web.WebSession;   
  21. import org.itrun.cnbrn.ConstantVar;   
  22.   
  23. import com.sun.image.codec.jpeg.JPEGCodec;   
  24. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  25.   
  26. public class ValidatorImageService implements IEngineService {   
  27.   
  28.     Log logger=LogFactory.getLog(this.getClass());   
  29.        
  30.     private HttpServletResponse response;   
  31.     private ServletContext context;   
  32.     private String TYPE="jpeg";   
  33.   
  34.     public void setResponse(HttpServletResponse response){   
  35.         this.response = response;   
  36.     }   
  37.        
  38.     public void setContext(ServletContext context){   
  39.         this.context=context;   
  40.     }   
  41.   
  42.     public ILink getLink(boolean arg0, Object arg1) {   
  43.         return null;   
  44.     }   
  45.   
  46.     public String getName() {   
  47.         return "validatorImage";   
  48.     }   
  49.   
  50.     public void service(IRequestCycle cycle) throws IOException {   
  51.         Random random = new Random();   
  52.         StringBuffer sb=new StringBuffer();   
  53.         for(int i=0;i<4;i++){   
  54.             int x=random.nextInt(25);   
  55.             x+=65;   
  56.             sb.append(String.valueOf((char)x));   
  57.             //sb.append(" ");   
  58.         }          
  59.         String validateString=sb.toString();           
  60.         WebSession sess=cycle.getInfrastructure().getRequest().getSession(true);   
  61.         sess.setAttribute(ConstantVar.VALIDATOR_SESSION_KEY, validateString);   
  62.         //画布大小   
  63.         int width = 60, height = 20;   
  64.         BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);   
  65.         Graphics g = image.getGraphics();   
  66.         // 以下填充背景颜色   
  67.         g.setColor(Color.decode("#FFFFFF"));   
  68.         g.fillRect(00, width, height);   
  69.         //随机产生其他元素,使图象中的认证码不易被其它程序探测到   
  70.         g.setColor(getRandColor(160200));   
  71.         for (int i = 0; i < 300; i++) {   
  72.             int x = random.nextInt(width);   
  73.             int y = random.nextInt(height);   
  74.             int xl = random.nextInt(12);   
  75.             int yl = random.nextInt(12);   
  76.             g.drawLine(x, y, x + xl, y + yl);   
  77.         }   
  78.            
  79.         //字的颜色   
  80.         g.setColor(Color.decode("#01556B"));   
  81.         //写的字的大小   
  82.         g.setFont(new Font(null, Font.BOLD, 18));   
  83.         //在画布上写字   
  84.         g.drawString(validateString, 216);   
  85.         g.dispose();   
  86.         OutputStream os = response.getOutputStream();   
  87.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);   
  88.         encoder.encode(image);   
  89.         os.close();   
  90.     }   
  91.        
  92.     private Color getRandColor(int fc,int bc){//给定范围获得随机颜色   
  93.         Random random = new Random();   
  94.         if(fc>255) fc=255;   
  95.         if(bc>255) bc=255;   
  96.         int r=fc+random.nextInt(bc-fc);   
  97.         int g=fc+random.nextInt(bc-fc);   
  98.         int b=fc+random.nextInt(bc-fc);   
  99.         return new Color(r,g,b);   
  100.     }   
  101. }   
  102.   

==============================
hivemodule.xml文件增加以下内容
代码
  1. <service-point id="ValidatorImageService" interface="org.apache.tapestry.engine.IEngineService">  
  2. invoke-factory>  
  3.           <construct class="org.itrun.cnbrn.view.pageclass.system.ValidatorImageService"/>  
  4. /invoke-factory>  
  5.  </service-point>  
  6.   
  7.  <contribution configuration-id="tapestry.services.ApplicationServices">  
  8.     <service name="validatorImage" object="service:ValidatorImageService"/>  
  9.  </contribution>  

============================
在页面上调用
代码
  1. <img jwcid="@Any" src="ognl:cnbrnContextPath+'/app?service=validatorImage'" />   
 

你可能感兴趣的:(service,Random,Class,tapestry,import,interface)