`
zds625
  • 浏览: 10858 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类

servlet生成验证码

    博客分类:
  • Java
阅读更多
package image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ImageCode extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ImageCode() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			int width = 50;
			int height = 20;
			response.setContentType("image/jpeg");
			String s = "";
			Random random = new Random();
			for(int i=0; i<4; i++) {
				s += String.valueOf(random.nextInt(9));
			}
			System.out.println("验证码:"+s);
			HttpSession session = request.getSession();
			session.setAttribute("validataCode", s);
			ServletOutputStream out = response.getOutputStream();
			BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
			Graphics g = image.getGraphics();
			g.setColor(getRandColor(200, 250));
			//填充矩形
			g.fillRect(0, 0, width, height);
			 Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体 
	         g.setFont(mFont); 
	         g.setColor(getRandColor(160, 200)); 
	         Random r = new Random();
	         //随机的生成一些线
	    	 for (int i = 0; i < 155; i++) { 
	             int x2 = r.nextInt(width); 
	             int y2 = r.nextInt(height); 
	             int x3 = r.nextInt(12); 
	             int y3 = r.nextInt(12); 
	             g.drawLine(x2, y2, x2 + x3, y2 + y3); 
	         } 
	
	    	 g.setColor(new Color(20 + random.nextInt(110), 20 + random 
	                 .nextInt(110), 20 + random.nextInt(110))); 
	
	         g.drawString(s, 2, 16); 
	         g.dispose(); 
	         // 输出图象到页面 
	         ImageIO.write((BufferedImage) image, "JPEG", out); 
	         out.close(); 
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	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); 
    }  


	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}


WEB.XML配置
  <servlet>
    <servlet-name>ImageCode</servlet-name>
    <servlet-class>image.ImageCode</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ImageCode</servlet-name>
    <url-pattern>/servlet/ImageCode</url-pattern>
  </servlet-mapping>

页面显示,需要jquery
<script type="text/javascript" src="js/jquery-1.4.js"></script>
<img id="vcode_img" src="<%=request.getContextPath() %>/servlet/ImageCode">
<a href="#" onClick="$('#vcode_img').attr('src','<%=request.getContextPath() %>/servlet/ImageCode?t='+(new Date().getTime()));return false;">看不清楚,换一张</a>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics