登录校验码

登录校验码:

============1.servlet======

/*登录校验码*/

@SuppressWarnings("serial")
public class SafeCode extends HttpServlet {
public SafeCode() {
System.out.println("---------- SafeCode --无参数构造方法SafeCode()--");
}  //


public void init() throws ServletException {   //
System.out.println("---------- SafeCode --init()--");
super.init();
}


public void doGet(HttpServletRequest request, HttpServletResponse response) //四种方法的执行时间是什么时候??
throws ServletException, IOException {

System.out.println("---------- SafeCode --doget()--");

response.setContentType("image/jpeg");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0L);
HttpSession session = request.getSession();
int width = 60;
int height = 20;
BufferedImage image = new BufferedImage(width, height, 1);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Arial", 0, 19));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width + 100);
int y = random.nextInt(height + 100);
int xl = random.nextInt(10);
int yl = random.nextInt(12);
g.drawOval(x, y, x + xl, y + yl);
}


String sRand = "";
for (int i = 0; i < 4; i++) {  //显示位数
String rand = getRandChar(random.nextInt(36));
sRand = sRand + rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}


session.setAttribute("rand", sRand);  //加入会话???
g.dispose();
javax.servlet.ServletOutputStream imageOut = response.getOutputStream();

//程序出错了 ,注解时间2018-3-1
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(imageOut);  //创建一个和指定输出流关联的JPEGImageEncoder对象
encoder.encode(image);
}


public void destroy() {  //
System.out.println("---------- SafeCode --destroy()--");
}  


private Color getRandColor(int fc, int bc) {
System.out.println("---------- SafeCode --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);
}


private String getRandChar(int randNumber) {
System.out.println("---------- SafeCode --String getRandChar(int randNumber)--");

return CHARARRAY[randNumber];
}


//private static final String CONTENT_TYPE = "image/jpeg";
private static final String CHARARRAY[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z" };


}

===========2.web.xml

<servlet>
    <servlet-name>safecode</servlet-name>
    <servlet-class>com.cjg.util.SafeCode</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>safecode</servlet-name>
    <url-pattern>/safecode</url-pattern>

  </servlet-mapping>

===========3.html===

前端显示

============4.action配置校验===

在此获校验码取数据。

猜你喜欢

转载自blog.csdn.net/qq_32444825/article/details/80405378