生成简单验证码的JSP页面
2017-03-16 22:07
393 查看
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Random"%> <%@ page import="java.io.OutputStream"%> <%@ page import="java.awt.Color"%> <%@ page import="java.awt.Font"%> <%@ page import="java.awt.Graphics"%> <%@ page import="java.awt.image.BufferedImage"%> <%@ page import="javax.imageio.ImageIO"%> <% int width = 80; int height = 32; //create the image BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // set the background color g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, width, height); // draw the border g.setColor(Color.black); g.drawRect(0, 0, width - 1, height - 1); // create a random instance to generate the codes Random rdm = new Random(); String hash1 = Integer.toHexString(rdm.nextInt()); // make some confusion for (int i = 0; i < 50; i++) { int x = rdm.nextInt(width); int y = rdm.nextInt(height); g.drawOval(x, y, 0, 0); } // generate a random code String capstr = hash1.substring(0, 4); session.setAttribute("key", capstr); //将验证码放到了session中 g.setColor(new Color(0, 100, 0)); g.setFont(new Font("Candara", Font.BOLD, 24)); g.drawString(capstr, 8, 24); g.dispose(); response.setContentType("image/jpeg"); out.clear(); out = pageContext.pushBody(); OutputStream strm = response.getOutputStream(); ImageIO.write(image, "jpeg", strm); strm.close(); %>
用法:
直接在需要验证码的地方请求这个jsp页面就可以了.
相关文章推荐
- 使用jsp生成验证码使用简单在java或jsp页面调用简单
- Jsp生成页面验证码的方法[附代码]
- JSP页面验证码生成
- JSP页面的验证码生成,验证
- Servlet生成JSP页面验证码
- JSP页面的验证码生成及调用方法
- JSP验证码简单生成方法
- Jsp页面验证码的生成方法
- Servlet生成验证码在JSP页面显示,单击验证码图片可刷新
- 在JSP页面中动态生成图片(验证码…
- jsp页面动态生成验证码
- Java 类生成验证码(数字字母),Jsp页面完成校验
- 简单的jsp生成验证码。。。
- Jsp生成页面验证码的方法
- Java 使用JSP页面生成随机验证码
- JSP简单计算验证码生成
- Jsp+Ajax生成页面验证码
- 公共代码之验证码jsp页面的生成和调用
- JSP验证码简单生成方法
- JSP页面生成验证码