验证码

package org.sunxin.ch22; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; 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 RandomCodeServlet extends HttpServlet { private int width=60; private int height=20; public RandomCodeServlet() { super(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage buffImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); Random random=new Random(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); Font font = new Font("Times New Roman",Font.PLAIN,18); g.setFont(font); g.setColor(Color.BLACK); g.drawRect(0, 0, width, height); g.setColor(Color.GRAY); for(int i=0;i<160;i++){ int x=random.nextInt(width); int y=random.nextInt(height); int x1=random.nextInt(12); int y1=random.nextInt(12); g.drawLine(x, y, x+x1, y+y1); } StringBuilder randomCode=new StringBuilder(); int red=0,green=0,blue=0; for(int i=0;i<4;i++){ String strRand=String.valueOf(random.nextInt(10)); red=random.nextInt(110); green=random.nextInt(50); blue=random.nextInt(50); g.setColor(new Color(red,green,blue)); g.drawString(strRand,13*i+6,16); randomCode.append(strRand); } HttpSession session=request.getSession(); session.setAttribute("randomCode", randomCode.toString()); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ServletOutputStream outputStream = response.getOutputStream(); javax.imageio.ImageIO.write(buffImg, "jpeg", outputStream); outputStream.close(); } }

你可能感兴趣的:(session,String,service,Random,Class,import)